|
1 | 1 | package org.baeldung.demo.boottest; |
2 | 2 |
|
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.hamcrest.CoreMatchers.is; |
| 5 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 6 | +import static org.hamcrest.Matchers.hasSize; |
| 7 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 8 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 9 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 10 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 11 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 12 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.List; |
| 16 | + |
3 | 17 | import org.baeldung.demo.DemoApplication; |
4 | | -import org.baeldung.demo.boottest.Employee; |
5 | | -import org.baeldung.demo.boottest.EmployeeRepository; |
6 | 18 | import org.junit.After; |
7 | 19 | import org.junit.Test; |
8 | 20 | import org.junit.runner.RunWith; |
9 | 21 | import org.springframework.beans.factory.annotation.Autowired; |
10 | 22 | import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; |
11 | 23 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
12 | 24 | import org.springframework.boot.test.context.SpringBootTest; |
13 | | -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
14 | 25 | import org.springframework.http.MediaType; |
15 | 26 | import org.springframework.test.context.junit4.SpringRunner; |
16 | 27 | import org.springframework.test.web.servlet.MockMvc; |
17 | 28 |
|
18 | | -import java.io.IOException; |
19 | | -import java.util.List; |
20 | | - |
21 | | -import static org.assertj.core.api.Assertions.assertThat; |
22 | | -import static org.hamcrest.CoreMatchers.is; |
23 | | -import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
24 | | -import static org.hamcrest.Matchers.hasSize; |
25 | | -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
26 | | -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
27 | | -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
28 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
29 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
30 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
31 | | - |
32 | 29 | @RunWith(SpringRunner.class) |
33 | | -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = DemoApplication.class) |
| 30 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = DemoApplication.class) |
34 | 31 | @AutoConfigureMockMvc |
35 | 32 | // @TestPropertySource(locations = "classpath:application-integrationtest.properties") |
36 | 33 | @AutoConfigureTestDatabase |
@@ -58,14 +55,22 @@ public void whenValidInput_thenCreateEmployee() throws IOException, Exception { |
58 | 55 |
|
59 | 56 | @Test |
60 | 57 | public void givenEmployees_whenGetEmployees_thenStatus200() throws Exception { |
61 | | - |
62 | 58 | createTestEmployee("bob"); |
63 | 59 | createTestEmployee("alex"); |
64 | 60 |
|
65 | | - mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$", hasSize(greaterThanOrEqualTo(2)))) |
66 | | - .andExpect(jsonPath("$[0].name", is("bob"))).andExpect(jsonPath("$[1].name", is("alex"))); |
| 61 | + // @formatter:off |
| 62 | + mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON)) |
| 63 | + .andDo(print()) |
| 64 | + .andExpect(status().isOk()) |
| 65 | + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) |
| 66 | + .andExpect(jsonPath("$", hasSize(greaterThanOrEqualTo(2)))) |
| 67 | + .andExpect(jsonPath("$[0].name", is("bob"))) |
| 68 | + .andExpect(jsonPath("$[1].name", is("alex"))); |
| 69 | + // @formatter:on |
67 | 70 | } |
68 | 71 |
|
| 72 | + // |
| 73 | + |
69 | 74 | private void createTestEmployee(String name) { |
70 | 75 | Employee emp = new Employee(name); |
71 | 76 | repository.saveAndFlush(emp); |
|
0 commit comments