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
+ */
1
16
package org .springframework .data .r2dbc .documentation ;
2
17
18
+ import static org .mockito .Mockito .*;
3
19
import 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 ;
5
21
22
+ import lombok .AllArgsConstructor ;
6
23
import lombok .Data ;
7
24
import lombok .NoArgsConstructor ;
8
25
import reactor .core .publisher .Flux ;
26
+ import reactor .test .StepVerifier ;
9
27
10
28
import org .junit .jupiter .api .Test ;
11
29
import org .springframework .data .annotation .Id ;
12
30
import org .springframework .data .domain .Example ;
13
31
import org .springframework .data .domain .ExampleMatcher ;
14
32
import org .springframework .data .r2dbc .repository .R2dbcRepository ;
15
33
34
+ /**
35
+ * Code to demonstrate Query By Example in reference documentation.
36
+ *
37
+ * @since 1.3
38
+ * @author Greg Turnquist
39
+ */
16
40
public class QueryByExampleTests {
17
41
18
42
private EmployeeRepository repository ;
19
43
20
44
@ Test
21
45
void queryByExampleSimple () {
22
46
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
+
23
53
// tag::example[]
24
54
Employee employee = new Employee (); // <1>
25
55
employee .setName ("Frodo" );
@@ -30,11 +60,23 @@ void queryByExampleSimple() {
30
60
31
61
// do whatever with the flux
32
62
// end::example[]
63
+
64
+ employees //
65
+ .as (StepVerifier ::create ) //
66
+ .expectNext (new Employee (1 , "Frodo" , "ring bearer" )) //
67
+ .verifyComplete ();
33
68
}
34
69
35
70
@ Test
36
71
void queryByExampleCustomMatcher () {
37
72
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
+
38
80
// tag::example-2[]
39
81
Employee employee = new Employee ();
40
82
employee .setName ("Baggins" );
@@ -50,10 +92,17 @@ void queryByExampleCustomMatcher() {
50
92
51
93
// do whatever with the flux
52
94
// 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 ();
53
101
}
54
102
55
103
@ Data
56
104
@ NoArgsConstructor
105
+ @ AllArgsConstructor
57
106
public class Employee {
58
107
59
108
private @ Id Integer id ;
0 commit comments