|
1 | 1 | package com.baeldung.reactive.functional; |
2 | 2 |
|
| 3 | +import static org.mockito.BDDMockito.given; |
| 4 | +import static org.mockito.Mockito.verify; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.junit.FixMethodOrder; |
3 | 10 | import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | +import org.junit.runners.MethodSorters; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.context.SpringBootTest; |
| 15 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 16 | +import org.springframework.test.context.junit4.SpringRunner; |
4 | 17 | import org.springframework.test.web.reactive.server.WebTestClient; |
5 | 18 |
|
6 | 19 | import com.baeldung.webflux.Employee; |
| 20 | +import com.baeldung.webflux.EmployeeRepository; |
7 | 21 |
|
| 22 | +import reactor.core.publisher.Flux; |
| 23 | +import reactor.core.publisher.Mono; |
| 24 | + |
| 25 | +@RunWith(SpringRunner.class) |
| 26 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = EmployeeSpringFunctionalApplication.class) |
| 27 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
8 | 28 | public class EmployeeSpringFunctionalIntegrationTest { |
9 | 29 |
|
10 | | - private static EmployeeFunctionalConfig config = new EmployeeFunctionalConfig(); |
| 30 | + @Autowired |
| 31 | + private EmployeeFunctionalConfig config; |
| 32 | + |
| 33 | + @MockBean |
| 34 | + private EmployeeRepository employeeRepository; |
11 | 35 |
|
12 | 36 | @Test |
13 | 37 | public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() { |
14 | 38 | WebTestClient client = WebTestClient |
15 | 39 | .bindToRouterFunction(config.getEmployeeByIdRoute()) |
16 | 40 | .build(); |
17 | 41 |
|
18 | | - Employee expected = new Employee("1", "Employee 1"); |
| 42 | + Employee employee = new Employee("1", "Employee 1"); |
| 43 | + |
| 44 | + given(employeeRepository.findEmployeeById("1")).willReturn(Mono.just(employee)); |
19 | 45 |
|
20 | 46 | client.get() |
21 | 47 | .uri("/employees/1") |
22 | 48 | .exchange() |
23 | 49 | .expectStatus() |
24 | 50 | .isOk() |
25 | 51 | .expectBody(Employee.class) |
26 | | - .isEqualTo(expected); |
| 52 | + .isEqualTo(employee); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void whenGetAllEmployees_thenCorrectEmployees() { |
| 57 | + WebTestClient client = WebTestClient |
| 58 | + .bindToRouterFunction(config.getAllEmployeesRoute()) |
| 59 | + .build(); |
| 60 | + |
| 61 | + List<Employee> employeeList = new ArrayList<>(); |
| 62 | + |
| 63 | + Employee employee1 = new Employee("1", "Employee 1"); |
| 64 | + Employee employee2 = new Employee("2", "Employee 2"); |
| 65 | + |
| 66 | + employeeList.add(employee1); |
| 67 | + employeeList.add(employee2); |
| 68 | + |
| 69 | + Flux<Employee> employeeFlux = Flux.fromIterable(employeeList); |
| 70 | + given(employeeRepository.findAllEmployees()).willReturn(employeeFlux); |
| 71 | + |
| 72 | + client.get() |
| 73 | + .uri("/employees") |
| 74 | + .exchange() |
| 75 | + .expectStatus() |
| 76 | + .isOk() |
| 77 | + .expectBodyList(Employee.class) |
| 78 | + .isEqualTo(employeeList); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void whenUpdateEmployee_thenEmployeeUpdated() { |
| 83 | + WebTestClient client = WebTestClient |
| 84 | + .bindToRouterFunction(config.updateEmployeeRoute()) |
| 85 | + .build(); |
| 86 | + |
| 87 | + Employee employee = new Employee("1", "Employee 1 Updated"); |
| 88 | + |
| 89 | + client.post() |
| 90 | + .uri("/employees/update") |
| 91 | + .body(Mono.just(employee), Employee.class) |
| 92 | + .exchange() |
| 93 | + .expectStatus() |
| 94 | + .isOk(); |
| 95 | + |
| 96 | + verify(employeeRepository).updateEmployee(employee); |
27 | 97 | } |
28 | 98 | } |
0 commit comments