Spring Boot REST Test Using Mock MVC WebAppContextSetup Method

Spring Boot REST Test Using Mock MVC WebAppContextSetup Method

In this REST example we will use same project that we created earlier.To create project you can follow below link.

REST API Example Using Spring Boot


Or you can download above project from given link.

Spring Boot REST Example


To test any spring web application, we need to test that method is properly handling Request methods like GET, POST, so to test it we need to request HTTP and need to check that it process correctly.

Spring provides two ways by which we can test above scenario.

  • Mock MVC : Server is not started in it and it is mocked
  • Web Integration Test : Application is started in embedded server.So it is slower but actual test

In this example we are using Mock MVC

To test GET API we are using following class which contains a GET API

 
package com.example.preparationforinterview.FirstSpringBootPreparation;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestService {	
	
	@RequestMapping(value = "/api", method = RequestMethod.GET)
	public String getMessage(@RequestParam("name") String name){
		return "Hello" + " " + name;		
	}
}

If we have RestService on separate package then we need to include @Import({ RestService.class }) in spring boot class , in our example


 
@SpringBootApplication
@Import({ RestService.class })
public class FirstSpringBootPreparationApplication {

Now create on class to test above API say FirstSpringBootPreparationApplicationTestsByWebAppConfiguration and copy code

 
package com.example.preparationforinterview.FirstSpringBootPreparation;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { RestService.class })
@WebAppConfiguration
public class FirstSpringBootPreparationApplicationTestsByWebAppConfiguration {

	@Autowired
	private WebApplicationContext webContext;
	private MockMvc mockMvc;

	@Test
	public void contextLoads() throws Exception {
		mockMvc = MockMvcBuilders.webAppContextSetup(webContext).build();

		mockMvc.perform(
				MockMvcRequestBuilders.get("/api").param("name", "developer"))
				.andExpect(MockMvcResultMatchers.status().isOk())
				.andExpect(MockMvcResultMatchers.view().name("Hello developer"));
	}

}

Right click on FirstSpringBootPreparationApplicationTestsByWebAppConfiguration and run as Junit. It will pass test case.

Explain :

Mock MVC provides MockMvcBuilders, that provides two methods

To test using webAppContextSetup() we need to annotate our test class with @WebAppConfiguration.That provides WebApplicationContext instance.WebApplicationContext is passed into webAppContextSetup method.

When we call build method , it gives us an instance of MockMvc

On MockMvc we perform API call and test that is it properly called checking status and response if any.

Download source code

Spring Boot REST API Test with Mock MVC Using webAppContextSetup



Visit Others Links

Spring Boot Introduction
Spring Boot Setup
REST API Example Using Spring Boot
Spring Boot REST API Test with Mock MVC Using standaloneSetup
Spring Boot REST API Test with Mock MVC Using webAppContextSetup
Spring Boot REST API Test with Embedded Server
Actuator In Spring Boot
Deployment Of Spring Boot In External Tomcat
Spring Boot with OAuth
Top Spring Boot Interview Question