Develop the Presentation Tier
This is the final step in the service development process.
We'll cover the following...
REST-based Spring Controller
The final level in the stack is the REST-based Spring Controller. First, we will look at how to write the test and then follow it with the corresponding controller.
Press + to interact
@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfiguration( locations = { "classpath:context.xml" } )@TransactionConfiguration(defaultRollback = true)@Transactionalpublic class EstateControllerTest {private Gson gson = new GsonBuilder().create();@Resourceprivate WebApplicationContext webApplicationContext;private MockMvc mockMvc;@Beforepublic void setUp() {mockMvc = MockMvcBuilders.<StandaloneMockMvcBuilder>webAppContextSetup(webApplicationContext).build();}@Testpublic void testAll() throws Exception {testCreate();testUpdate();testDelete();}public void testCreate() throws Exception {EstateDto estateDto = new BuildingDto();estateDto.setName("ABC");((BuildingDto)estateDto).setFloors(20);String json = gson.toJson(estateDto);json= json.replace("{", "");json= json.replace("}", "");json = "{\"type\":\"building\","+json+"}";MockHttpServletRequestBuilder requestBuilderOne = MockMvcRequestBuilders.post("/concretetableinheritance/create");requestBuilderOne.contentType(MediaType.APPLICATION_JSON);requestBuilderOne.content(json.getBytes());this.mockMvc.perform(requestBuilderOne).andExpect(MockMvcResultMatchers.status().isOk());}public void testUpdate() throws Exception {MockHttpServletRequestBuilder requestBuilder2 = MockMvcRequestBuilders.get("/concretetableinheritance/findAll");MvcResult result = this.mockMvc.perform(requestBuilder2).andReturn();String response2 = result.getResponse().getContentAsString();Type listType = new TypeToken<EstateDto[]>() {}.getType();EstateDto[] estateDtoList = gson.fromJson(response2, listType);EstateDto estateDto2 = estateDtoList[0];EstateDto newEstateDto = new BuildingDto();newEstateDto.setId(estateDto2.getId());newEstateDto.setName("DEF");((BuildingDto)newEstateDto).setFloors(21);String json2 = gson.toJson(newEstateDto);json2= json2.replace("{", "");json2= json2.replace("}", "");json2 = "{\"type\":\"building\","+json2+"}";MockHttpServletRequestBuilder requestBuilder3 = MockMvcRequestBuilders.post("/concretetableinheritance/edit");requestBuilder3.contentType(MediaType.APPLICATION_JSON);requestBuilder3.content(json2.getBytes());this.mockMvc.perform(requestBuilder3).andExpect(MockMvcResultMatchers.status().isOk());}public void testDelete() throws Exception {MockHttpServletRequestBuilder requestBuilder2 = MockMvcRequestBuilders.get("/concretetableinheritance/findAll");MvcResult result = this.mockMvc.perform(requestBuilder2).andReturn();String response2 = result.getResponse().getContentAsString();Type listType = new TypeToken<EstateDto[]>() {}.getType();EstateDto[] estateDtoList = gson.fromJson(response2, listType);EstateDto estateDto2 = estateDtoList[0];MockHttpServletRequestBuilder requestBuilder3 = MockMvcRequestBuilders.post("/concretetableinheritance/remove/"+estateDto2.getId());requestBuilder3.contentType(MediaType.APPLICATION_JSON);this.mockMvc.perform(requestBuilder3).andExpect(MockMvcResultMatchers.status().is(204));}}
...