Let us implement test cases for each RESTful service of a microservice by performing the following steps:
- Just like in the previous recipe, add the needed Spring Test starter POM dependency in the project's pom.xml file.
- Inside src/test/java, create an org.packt.microservice.core.test package and drop a test class that executes blocking RESTful services using org.springframework.boot.test.web.client.TestRestTemplate:
import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @ContextConfiguration(classes={ HRDeptBootApplication.class, CacheConfig.class, SpringDataConfig.class, SpringAsynchConfig.class }) public class TestRestService { @Autowired private TestRestTemplate restTemplate; @Test public void exampleTest() { String body = this.restTemplate.getForObject( "/objectSampleRest", String.class); assertThat(body).isEqualTo("Hello World"); } @Test public void exampleTestList() { String body = this.restTemplate.getForObject( "/objectSampleRest", String.class); assertThat(body).isEqualTo("Hello World"); } @Test public void exampleTestListAll() { List<Department> body = this.restTemplate.getForObject( "/listDept", List.class); assertNotNull(body); System.out.println(body); } }
- Add another test class that scrutinizes asynchronous services that return the Callable<T>, DeferredResult<T>, and WebAsyncTask<T> tasks:
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(SpringRunner.class) @AutoConfigureMockMvc @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @ContextConfiguration(classes={ HRDeptBootApplication.class, CacheConfig.class, SpringDataConfig.class, SpringAsynchConfig.class }) public class TestAsyncService { @Autowired private MockMvc mockMvc; @Test public void testController () throws Exception { MvcResult result = mockMvc.perform( get("/callSelectDept/359.json")) .andExpect(request().asyncStarted()) .andReturn(); result.getRequest().getAsyncContext() .setTimeout(5000); result.getAsyncResult(); result= mockMvc.perform(asyncDispatch(result)) .andExpect(status().isOk()) .andExpect(header().string("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE)) .andReturn(); System.out.println(result.getResponse() .getContentAsString()); } }
Although there is AsyncRestTemplate, there is no corresponding test API for this as of the moment.
- Lastly, add a Spring TestContext class that evaluates reactive RESTful services exposed through either the @RestController or HandlerRouter<T> implementation. This test class uses the new API org.springframework.test.web.reactive.server.WebTestClient from Spring 5:
import static org.junit.Assert.assertEquals; @RunWith(SpringRunner.class) @AutoConfigureMockMvc @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @ContextConfiguration(classes={ HRDeptBootApplication.class, HttpServerConfig.class, SpringDataConfig.class, SpringAsynchConfig.class }) public class TestReactService { @Autowired private WebTestClient webTestClient; @Test public void testDeptById(){ FluxExchangeResult<Department> result = webTestClient.get() .uri("http://localhost:8090/ch10- dept/selectReactDept/359") .accept(MediaType.APPLICATION_JSON_UTF8) .exchange().returnResult(Department.class); assertEquals( result.getStatus().value(), 200); Department dept = result.getResponseBody().blockFirst(); System.out.println(dept.getName()); } @Test public void testDeptByIdRouter(){ FluxExchangeResult<Department> result = webTestClient.get() .uri("http://localhost:8901/selectDeptById/359") .accept(MediaType.APPLICATION_JSON_UTF8) .exchange().returnResult(Department.class); assertEquals( result.getStatus().value(), 200); Department dept = result.getResponseBody().blockFirst(); System.out.println(dept.getName()); } }