Merge pull request #8232 from sumit-bhawsar/BAEL-3335

Bael 3335 fixing integration tests
This commit is contained in:
Josh Cummings 2019-11-23 12:47:17 -07:00 committed by GitHub
commit d26649164b
1 changed files with 39 additions and 21 deletions

View File

@ -1,43 +1,61 @@
package org.baeldung.cachedrequest; 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.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 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) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = { HttpRequestDemoConfig.class, ContentCachingFilter.class, PrintRequestAttribute.class }) @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = { HttpRequestDemoConfig.class, ContentCachingFilter.class, PrintRequestContentFilter.class, PersonController.class })
@AutoConfigureMockMvc @WebAppConfiguration
public class PersonControllerIntegrationTest { public class PersonControllerIntegrationTest {
@Autowired @Autowired
private WebApplicationContext wac;
private MockMvc mockMvc; private MockMvc mockMvc;
ObjectMapper objectMapper = new ObjectMapper(); 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 @Test
public void whenValidInput_thenCreateBook() throws IOException, Exception { public void whenValidInput_thenCreateBook() throws IOException, Exception {
// assign - given // assign - given
Person book = new Person("sumit", "abc", 100); Person person = new Person("sumit", "abc", 100);
// act - when // act - when
ResultActions result = mockMvc.perform(post("/person").accept(MediaType.APPLICATION_JSON) ResultActions result = mockMvc.perform(post("/person").accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(book))); .content(objectMapper.writeValueAsString(person)));
// assert - then // assert - then
result.andExpect(status().isNoContent()); result.andExpect(status().isNoContent());