From 892f75fc67153cb6214ea641bf8ef958ddfe52e9 Mon Sep 17 00:00:00 2001 From: sumit-bhawsar Date: Sat, 23 Nov 2019 12:28:07 +0000 Subject: [PATCH] BAEL-3335 fixing PersonControllerIntegrationTest --- .../PersonControllerIntegrationTest.java | 60 ++++++++++++------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/spring-core-2/src/test/java/org/baeldung/cachedrequest/PersonControllerIntegrationTest.java b/spring-core-2/src/test/java/org/baeldung/cachedrequest/PersonControllerIntegrationTest.java index 77d6a816e3..046a310cc0 100644 --- a/spring-core-2/src/test/java/org/baeldung/cachedrequest/PersonControllerIntegrationTest.java +++ b/spring-core-2/src/test/java/org/baeldung/cachedrequest/PersonControllerIntegrationTest.java @@ -1,43 +1,61 @@ package org.baeldung.cachedrequest; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.AnnotationConfigWebContextLoader; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.io.IOException; + import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import java.io.IOException; - -import javax.print.attribute.PrintRequestAttribute; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.MediaType; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.ResultActions; - -import com.fasterxml.jackson.databind.ObjectMapper; - @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = { HttpRequestDemoConfig.class, ContentCachingFilter.class, PrintRequestAttribute.class }) -@AutoConfigureMockMvc +@ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = { HttpRequestDemoConfig.class, ContentCachingFilter.class, PrintRequestContentFilter.class, PersonController.class }) +@WebAppConfiguration public class PersonControllerIntegrationTest { @Autowired + private WebApplicationContext wac; + private MockMvc mockMvc; ObjectMapper objectMapper = new ObjectMapper(); + @Autowired + private ContentCachingFilter contentCachingFilter; + + @Autowired + private PrintRequestContentFilter printRequestContentFilter; + + @Before + public void setup() throws Exception { + + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) + .addFilter(contentCachingFilter, "/**") + .addFilter(printRequestContentFilter, "/**") + .build(); + } + @Test public void whenValidInput_thenCreateBook() throws IOException, Exception { // assign - given - Person book = new Person("sumit", "abc", 100); + Person person = new Person("sumit", "abc", 100); // act - when ResultActions result = mockMvc.perform(post("/person").accept(MediaType.APPLICATION_JSON) - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(book))); + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(person))); // assert - then result.andExpect(status().isNoContent());