1+ /*
2+ * Copyright 2021 the original author or authors.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * https://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
116package org .springframework .data .r2dbc .documentation ;
217
18+ import static org .mockito .Mockito .*;
319import static org .springframework .data .domain .ExampleMatcher .*;
4- import static org .springframework .data .domain .ExampleMatcher .GenericPropertyMatchers .* ;
20+ import static org .springframework .data .domain .ExampleMatcher .GenericPropertyMatchers .endsWith ;
521
22+ import lombok .AllArgsConstructor ;
623import lombok .Data ;
724import lombok .NoArgsConstructor ;
825import reactor .core .publisher .Flux ;
26+ import reactor .test .StepVerifier ;
927
1028import org .junit .jupiter .api .Test ;
1129import org .springframework .data .annotation .Id ;
1230import org .springframework .data .domain .Example ;
1331import org .springframework .data .domain .ExampleMatcher ;
1432import org .springframework .data .r2dbc .repository .R2dbcRepository ;
1533
34+ /**
35+ * Code to demonstrate Query By Example in reference documentation.
36+ *
37+ * @since 1.3
38+ * @author Greg Turnquist
39+ */
1640public class QueryByExampleTests {
1741
1842 private EmployeeRepository repository ;
1943
2044 @ Test
2145 void queryByExampleSimple () {
2246
47+ this .repository = mock (EmployeeRepository .class );
48+
49+ when (this .repository .findAll ((Example <Employee >) any ())) //
50+ .thenReturn (Flux .just ( //
51+ new Employee (1 , "Frodo" , "ring bearer" )));
52+
2353 // tag::example[]
2454 Employee employee = new Employee (); // <1>
2555 employee .setName ("Frodo" );
@@ -30,11 +60,23 @@ void queryByExampleSimple() {
3060
3161 // do whatever with the flux
3262 // end::example[]
63+
64+ employees //
65+ .as (StepVerifier ::create ) //
66+ .expectNext (new Employee (1 , "Frodo" , "ring bearer" )) //
67+ .verifyComplete ();
3368 }
3469
3570 @ Test
3671 void queryByExampleCustomMatcher () {
3772
73+ this .repository = mock (EmployeeRepository .class );
74+
75+ when (this .repository .findAll ((Example <Employee >) any ())) //
76+ .thenReturn (Flux .just ( //
77+ new Employee (1 , "Frodo Baggins" , "ring bearer" ), //
78+ new Employee (1 , "Bilbo Baggins" , "burglar" )));
79+
3880 // tag::example-2[]
3981 Employee employee = new Employee ();
4082 employee .setName ("Baggins" );
@@ -50,10 +92,17 @@ void queryByExampleCustomMatcher() {
5092
5193 // do whatever with the flux
5294 // end::example-2[]
95+
96+ employees //
97+ .as (StepVerifier ::create ) //
98+ .expectNext (new Employee (1 , "Frodo Baggins" , "ring bearer" )) //
99+ .expectNext (new Employee (1 , "Bilbo Baggins" , "burglar" )) //
100+ .verifyComplete ();
53101 }
54102
55103 @ Data
56104 @ NoArgsConstructor
105+ @ AllArgsConstructor
57106 public class Employee {
58107
59108 private @ Id Integer id ;
0 commit comments