BAEL-2304 Improve functional webflux article
- Updated tests and minor spell fix
This commit is contained in:
parent
41b2495dcc
commit
9a09cc3ce3
@ -39,7 +39,7 @@ public class EmployeeFunctionalConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
RouterFunction<ServerResponse> updateEmployee() {
|
RouterFunction<ServerResponse> updateEmployeeRoute() {
|
||||||
return route(POST("/employees/update"),
|
return route(POST("/employees/update"),
|
||||||
req -> req.body(toMono(Employee.class))
|
req -> req.body(toMono(Employee.class))
|
||||||
.doOnNext(employeeRepository()::updateEmployee)
|
.doOnNext(employeeRepository()::updateEmployee)
|
||||||
|
@ -1,13 +1,37 @@
|
|||||||
package com.baeldung.reactive.functional;
|
package com.baeldung.reactive.functional;
|
||||||
|
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.FixMethodOrder;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.MethodSorters;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
|
||||||
import com.baeldung.webflux.Employee;
|
import com.baeldung.webflux.Employee;
|
||||||
|
import com.baeldung.webflux.EmployeeRepository;
|
||||||
|
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = EmployeeSpringFunctionalApplication.class)
|
||||||
|
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||||
public class EmployeeSpringFunctionalIntegrationTest {
|
public class EmployeeSpringFunctionalIntegrationTest {
|
||||||
|
|
||||||
private static EmployeeFunctionalConfig config = new EmployeeFunctionalConfig();
|
@Autowired
|
||||||
|
private EmployeeFunctionalConfig config;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private EmployeeRepository employeeRepository;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() {
|
public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() {
|
||||||
@ -15,7 +39,9 @@ public class EmployeeSpringFunctionalIntegrationTest {
|
|||||||
.bindToRouterFunction(config.getEmployeeByIdRoute())
|
.bindToRouterFunction(config.getEmployeeByIdRoute())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Employee expected = new Employee("1", "Employee 1");
|
Employee employee = new Employee("1", "Employee 1");
|
||||||
|
|
||||||
|
given(employeeRepository.findEmployeeById("1")).willReturn(Mono.just(employee));
|
||||||
|
|
||||||
client.get()
|
client.get()
|
||||||
.uri("/employees/1")
|
.uri("/employees/1")
|
||||||
@ -23,6 +49,50 @@ public class EmployeeSpringFunctionalIntegrationTest {
|
|||||||
.expectStatus()
|
.expectStatus()
|
||||||
.isOk()
|
.isOk()
|
||||||
.expectBody(Employee.class)
|
.expectBody(Employee.class)
|
||||||
.isEqualTo(expected);
|
.isEqualTo(employee);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGetAllEmployees_thenCorrectEmployees() {
|
||||||
|
WebTestClient client = WebTestClient
|
||||||
|
.bindToRouterFunction(config.getAllEmployeesRoute())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
List<Employee> employeeList = new ArrayList<>();
|
||||||
|
|
||||||
|
Employee employee1 = new Employee("1", "Employee 1");
|
||||||
|
Employee employee2 = new Employee("2", "Employee 2");
|
||||||
|
|
||||||
|
employeeList.add(employee1);
|
||||||
|
employeeList.add(employee2);
|
||||||
|
|
||||||
|
Flux<Employee> employeeFlux = Flux.fromIterable(employeeList);
|
||||||
|
given(employeeRepository.findAllEmployees()).willReturn(employeeFlux);
|
||||||
|
|
||||||
|
client.get()
|
||||||
|
.uri("/employees")
|
||||||
|
.exchange()
|
||||||
|
.expectStatus()
|
||||||
|
.isOk()
|
||||||
|
.expectBodyList(Employee.class)
|
||||||
|
.isEqualTo(employeeList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUpdateEmployee_thenEmployeeUpdated() {
|
||||||
|
WebTestClient client = WebTestClient
|
||||||
|
.bindToRouterFunction(config.updateEmployeeRoute())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Employee employee = new Employee("1", "Employee 1 Updated");
|
||||||
|
|
||||||
|
client.post()
|
||||||
|
.uri("/employees/update")
|
||||||
|
.body(Mono.just(employee), Employee.class)
|
||||||
|
.exchange()
|
||||||
|
.expectStatus()
|
||||||
|
.isOk();
|
||||||
|
|
||||||
|
verify(employeeRepository).updateEmployee(employee);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user