BAEL-2620 | Update test with latest version of Spring 5 and JUnit 5
This commit is contained in:
parent
023d307f51
commit
caf864b8e7
|
@ -1,17 +1,13 @@
|
||||||
package com.baeldung.web.controller;
|
package com.baeldung.web.controller;
|
||||||
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
import com.baeldung.spring.web.config.WebConfig;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import javax.servlet.ServletContext;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.mock.web.MockServletContext;
|
import org.springframework.mock.web.MockServletContext;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.MvcResult;
|
import org.springframework.test.web.servlet.MvcResult;
|
||||||
|
@ -20,31 +16,34 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
import com.baeldung.spring.web.config.WebConfig;
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||||
|
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@ContextConfiguration(classes = {WebConfig.class})
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@ContextConfiguration(classes = { WebConfig.class, WebConfig.class })
|
|
||||||
public class GreetControllerIntegrationTest {
|
public class GreetControllerIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext wac;
|
private WebApplicationContext webApplicationContext;
|
||||||
|
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
private static final String CONTENT_TYPE = "application/json";
|
private static final String CONTENT_TYPE = "application/json";
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void setup() throws Exception {
|
public void setup() {
|
||||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenWac_whenServletContext_thenItProvidesGreetController() {
|
public void givenWac_whenServletContext_thenItProvidesGreetController() {
|
||||||
final ServletContext servletContext = wac.getServletContext();
|
final ServletContext servletContext = webApplicationContext.getServletContext();
|
||||||
Assert.assertNotNull(servletContext);
|
assertNotNull(servletContext);
|
||||||
Assert.assertTrue(servletContext instanceof MockServletContext);
|
assertTrue(servletContext instanceof MockServletContext);
|
||||||
Assert.assertNotNull(wac.getBean("greetController"));
|
assertNotNull(webApplicationContext.getBean("greetController"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -54,8 +53,12 @@ public class GreetControllerIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception {
|
public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
final MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get("/greet")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!")).andReturn();
|
final MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get("/greet"))
|
||||||
Assert.assertEquals(CONTENT_TYPE, mvcResult.getResponse().getContentType());
|
.andDo(print())
|
||||||
|
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||||
|
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"))
|
||||||
|
.andReturn();
|
||||||
|
assertEquals(CONTENT_TYPE, mvcResult.getResponse().getContentType());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -2,33 +2,27 @@ package com.baeldung.web.controller;
|
||||||
|
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.RestAssured;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.web.server.LocalServerPort;
|
|
||||||
import org.springframework.test.context.TestPropertySource;
|
import org.springframework.test.context.TestPropertySource;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
|
import static io.restassured.RestAssured.DEFAULT_PORT;
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.RestAssured.given;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@SpringBootTest(webEnvironment = DEFINED_PORT)
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
|
||||||
@TestPropertySource(properties = {"spring.main.allow-bean-definition-overriding=true", "server.servlet.context-path=/"})
|
@TestPropertySource(properties = {"spring.main.allow-bean-definition-overriding=true", "server.servlet.context-path=/"})
|
||||||
public class GreetControllerRealIntegrationTest {
|
public class GreetControllerRealIntegrationTest {
|
||||||
|
|
||||||
@LocalServerPort
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
RestAssured.port = port;
|
RestAssured.port = DEFAULT_PORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGreetURI_whenSendingReq_thenVerifyResponse() {
|
public void givenGreetURI_whenSendingReq_thenVerifyResponse() {
|
||||||
given().get("/greet")
|
given().get("/greet")
|
||||||
.then()
|
.then()
|
||||||
.statusCode(200);
|
.statusCode(200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,25 +1,22 @@
|
||||||
package com.baeldung.web.controller;
|
package com.baeldung.web.controller;
|
||||||
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
|
||||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
|
||||||
public class GreetControllerUnitTest {
|
public class GreetControllerUnitTest {
|
||||||
|
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
private static final String CONTENT_TYPE = "application/json";
|
private static final String CONTENT_TYPE = "application/json";
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void setup() {
|
void setUp() {
|
||||||
this.mockMvc = MockMvcBuilders.standaloneSetup(new GreetController()).build();
|
this.mockMvc = MockMvcBuilders.standaloneSetup(new GreetController()).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,12 +47,12 @@ public class GreetControllerUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception {
|
public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World!!!"));
|
this.mockMvc.perform(post("/greetWithPost")).andDo(print()).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World!!!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() throws Exception {
|
public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||||
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE))
|
this.mockMvc.perform(post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE))
|
||||||
.andExpect(jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(jsonPath("$.id").value(1));
|
.andExpect(jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(jsonPath("$.id").value(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue