28
28
import org .springframework .context .annotation .Bean ;
29
29
import org .springframework .context .annotation .Configuration ;
30
30
import org .springframework .context .annotation .Import ;
31
+ import org .springframework .dao .DataAccessException ;
31
32
import org .springframework .data .annotation .Id ;
32
33
import org .springframework .data .jdbc .repository .config .EnableJdbcRepositories ;
33
34
import org .springframework .data .jdbc .testing .TestConfiguration ;
38
39
import org .springframework .test .context .junit4 .rules .SpringMethodRule ;
39
40
import org .springframework .transaction .annotation .Transactional ;
40
41
42
+ import static org .assertj .core .api .Assertions .*;
43
+
41
44
/**
42
45
* Tests the execution of queries from {@link Query} annotations on repository methods.
43
46
*
@@ -97,10 +100,9 @@ public void executeCustomQueryWithReturnTypeIsOptional() {
97
100
@ Test // DATAJDBC-172
98
101
public void executeCustomQueryWithReturnTypeIsOptionalWhenEntityNotFound () {
99
102
100
- DummyEntity dummyEntity = dummyEntity ("a" );
101
- repository .save (dummyEntity );
103
+ repository .save (dummyEntity ("a" ));
102
104
103
- Optional <DummyEntity > entity = repository .findByIdWithReturnTypeIsOptional ( 9999L );
105
+ Optional <DummyEntity > entity = repository .findByNameAsOptional ( "x" );
104
106
105
107
assertThat (entity ).isNotPresent ();
106
108
@@ -109,16 +111,46 @@ public void executeCustomQueryWithReturnTypeIsOptionalWhenEntityNotFound() {
109
111
@ Test // DATAJDBC-172
110
112
public void executeCustomQueryWithReturnTypeIsEntity () {
111
113
112
- DummyEntity dummyEntity = dummyEntity ("a" );
113
- repository .save (dummyEntity );
114
+ repository .save (dummyEntity ("a" ));
114
115
115
- DummyEntity entity = repository .findByIdWithReturnTypeIsEntity ( dummyEntity . id );
116
+ DummyEntity entity = repository .findByNameAsEntity ( "a" );
116
117
117
118
assertThat (entity ).isNotNull ();
118
119
assertThat (entity .name ).isEqualTo ("a" );
119
120
120
121
}
121
122
123
+ @ Test // DATAJDBC-172
124
+ public void executeCustomQueryWithReturnTypeIsEntityWhenEntityNotFound () {
125
+
126
+ repository .save (dummyEntity ("a" ));
127
+
128
+ DummyEntity entity = repository .findByNameAsEntity ("x" );
129
+
130
+ assertThat (entity ).isNull ();
131
+
132
+ }
133
+
134
+ @ Test // DATAJDBC-172
135
+ public void executeCustomQueryWithReturnTypeIsEntityWhenEntityDuplicateResult () {
136
+
137
+ repository .save (dummyEntity ("a" ));
138
+ repository .save (dummyEntity ("a" ));
139
+
140
+ assertThatExceptionOfType (DataAccessException .class ) //
141
+ .isThrownBy (() -> repository .findByNameAsEntity ("a" ));
142
+ }
143
+
144
+ @ Test // DATAJDBC-172
145
+ public void executeCustomQueryWithReturnTypeIsOptionalWhenEntityDuplicateResult () {
146
+
147
+ repository .save (dummyEntity ("a" ));
148
+ repository .save (dummyEntity ("a" ));
149
+
150
+ assertThatExceptionOfType (DataAccessException .class ) //
151
+ .isThrownBy (() -> repository .findByNameAsOptional ("a" ));
152
+ }
153
+
122
154
@ Test // DATAJDBC-172
123
155
public void executeCustomQueryWithReturnTypeIsStream () {
124
156
@@ -165,12 +197,12 @@ private interface DummyEntityRepository extends CrudRepository<DummyEntity, Long
165
197
166
198
@ Query ("SELECT * FROM DUMMYENTITY WHERE name < :upper and name > :lower" )
167
199
List <DummyEntity > findByNamedRangeWithNamedParameter (@ Param ("lower" ) String lower , @ Param ("upper" ) String upper );
168
-
169
- @ Query ("SELECT * FROM DUMMYENTITY WHERE id = :id FOR UPDATE" )
170
- Optional <DummyEntity > findByIdWithReturnTypeIsOptional (@ Param ("id" ) Long id );
171
200
172
- @ Query ("SELECT * FROM DUMMYENTITY WHERE id = :id FOR UPDATE" )
173
- DummyEntity findByIdWithReturnTypeIsEntity (@ Param ("id" ) Long id );
201
+ @ Query ("SELECT * FROM DUMMYENTITY WHERE name = :name" )
202
+ Optional <DummyEntity > findByNameAsOptional (@ Param ("name" ) String name );
203
+
204
+ @ Query ("SELECT * FROM DUMMYENTITY WHERE name = :name" )
205
+ DummyEntity findByNameAsEntity (@ Param ("name" ) String name );
174
206
175
207
@ Query ("SELECT * FROM DUMMYENTITY" )
176
208
Stream <DummyEntity > findAllWithReturnTypeIsStream ();
0 commit comments