diff --git a/src/test/java/org/springframework/data/r2dbc/documentation/QueryByExampleTests.java b/src/test/java/org/springframework/data/r2dbc/documentation/QueryByExampleTests.java index 680be03c..66e77e83 100644 --- a/src/test/java/org/springframework/data/r2dbc/documentation/QueryByExampleTests.java +++ b/src/test/java/org/springframework/data/r2dbc/documentation/QueryByExampleTests.java @@ -1,11 +1,29 @@ +/* + * Copyright 2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.data.r2dbc.documentation; +import static org.mockito.Mockito.*; import static org.springframework.data.domain.ExampleMatcher.*; -import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.*; +import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.endsWith; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import reactor.core.publisher.Flux; +import reactor.test.StepVerifier; import org.junit.jupiter.api.Test; import org.springframework.data.annotation.Id; @@ -13,6 +31,12 @@ import org.springframework.data.domain.ExampleMatcher; import org.springframework.data.r2dbc.repository.R2dbcRepository; +/** + * Code to demonstrate Query By Example in reference documentation. + * + * @since 1.3 + * @author Greg Turnquist + */ public class QueryByExampleTests { private EmployeeRepository repository; @@ -20,6 +44,12 @@ public class QueryByExampleTests { @Test void queryByExampleSimple() { + this.repository = mock(EmployeeRepository.class); + + when(this.repository.findAll((Example) any())) // + .thenReturn(Flux.just( // + new Employee(1, "Frodo", "ring bearer"))); + // tag::example[] Employee employee = new Employee(); // <1> employee.setName("Frodo"); @@ -30,11 +60,23 @@ void queryByExampleSimple() { // do whatever with the flux // end::example[] + + employees // + .as(StepVerifier::create) // + .expectNext(new Employee(1, "Frodo", "ring bearer")) // + .verifyComplete(); } @Test void queryByExampleCustomMatcher() { + this.repository = mock(EmployeeRepository.class); + + when(this.repository.findAll((Example) any())) // + .thenReturn(Flux.just( // + new Employee(1, "Frodo Baggins", "ring bearer"), // + new Employee(1, "Bilbo Baggins", "burglar"))); + // tag::example-2[] Employee employee = new Employee(); employee.setName("Baggins"); @@ -50,10 +92,17 @@ void queryByExampleCustomMatcher() { // do whatever with the flux // end::example-2[] + + employees // + .as(StepVerifier::create) // + .expectNext(new Employee(1, "Frodo Baggins", "ring bearer")) // + .expectNext(new Employee(1, "Bilbo Baggins", "burglar")) // + .verifyComplete(); } @Data @NoArgsConstructor + @AllArgsConstructor public class Employee { private @Id Integer id;