|
| 1 | +package org.baeldung.boot.boottest; |
| 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 | + |
| 17 | +import org.baeldung.boot.DemoApplication; |
| 18 | +import org.junit.After; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 23 | +import org.springframework.boot.test.context.SpringBootTest; |
| 24 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 25 | +import org.springframework.http.MediaType; |
| 26 | +import org.springframework.test.context.TestPropertySource; |
| 27 | +import org.springframework.test.context.junit4.SpringRunner; |
| 28 | +import org.springframework.test.web.servlet.MockMvc; |
| 29 | + |
| 30 | +@RunWith(SpringRunner.class) |
| 31 | +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = DemoApplication.class) |
| 32 | +@AutoConfigureMockMvc |
| 33 | +@TestPropertySource(locations = "classpath:application-integrationtest.properties") |
| 34 | +public class EmployeeRestControllerIntTest { |
| 35 | + |
| 36 | + @Autowired |
| 37 | + MockMvc mvc; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + EmployeeRepository repository; |
| 41 | + |
| 42 | + @After |
| 43 | + public void resetDb() { |
| 44 | + repository.deleteAll(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void whenValidInput_thenCreateEmployee() throws IOException, Exception { |
| 49 | + Employee bob = new Employee("bob"); |
| 50 | + mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON) |
| 51 | + .content(JsonUtil.toJson(bob))); |
| 52 | + |
| 53 | + List<Employee> found = repository.findAll(); |
| 54 | + assertThat(found).extracting(Employee::getName) |
| 55 | + .containsOnly("bob"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void givenEmployees_whenGetEmployees_thenStatus200() throws Exception { |
| 60 | + |
| 61 | + createTestEmployee("bob"); |
| 62 | + createTestEmployee("alex"); |
| 63 | + |
| 64 | + mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON)) |
| 65 | + .andDo(print()) |
| 66 | + .andExpect(status().isOk()) |
| 67 | + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) |
| 68 | + .andExpect(jsonPath("$", hasSize(greaterThanOrEqualTo(2)))) |
| 69 | + .andExpect(jsonPath("$[0].name", is("bob"))) |
| 70 | + .andExpect(jsonPath("$[1].name", is("alex"))); |
| 71 | + } |
| 72 | + |
| 73 | + private void createTestEmployee(String name) { |
| 74 | + Employee emp = new Employee(name); |
| 75 | + repository.saveAndFlush(emp); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments