Skip to content

Commit 9a09cc3

Browse files
committed
BAEL-2304 Improve functional webflux article
- Updated tests and minor spell fix
1 parent 41b2495 commit 9a09cc3

2 files changed

Lines changed: 74 additions & 4 deletions

File tree

‎spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeFunctionalConfig.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ RouterFunction<ServerResponse> getEmployeeByIdRoute() {
3939
}
4040

4141
@Bean
42-
RouterFunction<ServerResponse> updateEmployee() {
42+
RouterFunction<ServerResponse> updateEmployeeRoute() {
4343
return route(POST("/employees/update"),
4444
req -> req.body(toMono(Employee.class))
4545
.doOnNext(employeeRepository()::updateEmployee)
Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,98 @@
11
package com.baeldung.reactive.functional;
22

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;
310
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;
417
import org.springframework.test.web.reactive.server.WebTestClient;
518

619
import com.baeldung.webflux.Employee;
20+
import com.baeldung.webflux.EmployeeRepository;
721

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)
828
public class EmployeeSpringFunctionalIntegrationTest {
929

10-
private static EmployeeFunctionalConfig config = new EmployeeFunctionalConfig();
30+
@Autowired
31+
private EmployeeFunctionalConfig config;
32+
33+
@MockBean
34+
private EmployeeRepository employeeRepository;
1135

1236
@Test
1337
public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() {
1438
WebTestClient client = WebTestClient
1539
.bindToRouterFunction(config.getEmployeeByIdRoute())
1640
.build();
1741

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));
1945

2046
client.get()
2147
.uri("/employees/1")
2248
.exchange()
2349
.expectStatus()
2450
.isOk()
2551
.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);
2797
}
2898
}

0 commit comments

Comments
 (0)