1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
34
34
import org .junit .jupiter .api .BeforeEach ;
35
35
import org .junit .jupiter .api .Test ;
36
36
37
- import org .springframework .jdbc .core .RowMapper ;
38
-
39
37
import static org .assertj .core .api .Assertions .assertThat ;
40
38
import static org .mockito .ArgumentMatchers .anyString ;
41
39
import static org .mockito .BDDMockito .given ;
@@ -135,8 +133,7 @@ public void testQueryForListWithParamMapAndSingleRowAndColumn() throws Exception
135
133
}
136
134
137
135
@ Test
138
- public void testQueryForListWithParamMapAndIntegerElementAndSingleRowAndColumn ()
139
- throws Exception {
136
+ public void testQueryForListWithParamMapAndIntegerElementAndSingleRowAndColumn () throws Exception {
140
137
given (resultSet .getMetaData ()).willReturn (resultSetMetaData );
141
138
given (resultSet .next ()).willReturn (true , false );
142
139
given (resultSet .getInt (1 )).willReturn (11 );
@@ -174,11 +171,10 @@ public void testQueryForObjectWithParamMapAndRowMapper() throws Exception {
174
171
175
172
MapSqlParameterSource params = new MapSqlParameterSource ();
176
173
params .addValue ("id" , 3 );
177
- Object o = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID = :id" ,
178
- params , (RowMapper < Object >) ( rs , rowNum ) -> rs .getInt (1 ));
174
+ Integer value = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID = :id" ,
175
+ params , (rs , rowNum ) -> rs .getInt (1 ));
179
176
180
- boolean condition = o instanceof Integer ;
181
- assertThat (condition ).as ("Correct result type" ).isTrue ();
177
+ assertThat (value ).isEqualTo (22 );
182
178
verify (connection ).prepareStatement ("SELECT AGE FROM CUSTMR WHERE ID = ?" );
183
179
verify (preparedStatement ).setObject (1 , 3 );
184
180
}
@@ -191,11 +187,10 @@ public void testQueryForObjectWithMapAndInteger() throws Exception {
191
187
192
188
Map <String , Object > params = new HashMap <>();
193
189
params .put ("id" , 3 );
194
- Object o = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID = :id" ,
190
+ Integer value = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID = :id" ,
195
191
params , Integer .class );
196
192
197
- boolean condition = o instanceof Integer ;
198
- assertThat (condition ).as ("Correct result type" ).isTrue ();
193
+ assertThat (value ).isEqualTo (22 );
199
194
verify (connection ).prepareStatement ("SELECT AGE FROM CUSTMR WHERE ID = ?" );
200
195
verify (preparedStatement ).setObject (1 , 3 );
201
196
}
@@ -208,30 +203,26 @@ public void testQueryForObjectWithParamMapAndInteger() throws Exception {
208
203
209
204
MapSqlParameterSource params = new MapSqlParameterSource ();
210
205
params .addValue ("id" , 3 );
211
- Object o = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID = :id" ,
206
+ Integer value = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID = :id" ,
212
207
params , Integer .class );
213
208
214
- boolean condition = o instanceof Integer ;
215
- assertThat (condition ).as ("Correct result type" ).isTrue ();
209
+ assertThat (value ).isEqualTo (22 );
216
210
verify (connection ).prepareStatement ("SELECT AGE FROM CUSTMR WHERE ID = ?" );
217
211
verify (preparedStatement ).setObject (1 , 3 );
218
212
}
219
213
220
214
@ Test
221
215
public void testQueryForObjectWithParamMapAndList () throws Exception {
222
- String sql = "SELECT AGE FROM CUSTMR WHERE ID IN (:ids)" ;
223
- String sqlToUse = "SELECT AGE FROM CUSTMR WHERE ID IN (?, ?)" ;
224
216
given (resultSet .getMetaData ()).willReturn (resultSetMetaData );
225
217
given (resultSet .next ()).willReturn (true , false );
226
218
given (resultSet .getInt (1 )).willReturn (22 );
227
219
228
220
MapSqlParameterSource params = new MapSqlParameterSource ();
229
221
params .addValue ("ids" , Arrays .asList (3 , 4 ));
230
- Object o = template .queryForObject (sql , params , Integer .class );
222
+ Integer value = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE ID IN (:ids)" , params , Integer .class );
231
223
232
- boolean condition = o instanceof Integer ;
233
- assertThat (condition ).as ("Correct result type" ).isTrue ();
234
- verify (connection ).prepareStatement (sqlToUse );
224
+ assertThat (value ).isEqualTo (22 );
225
+ verify (connection ).prepareStatement ("SELECT AGE FROM CUSTMR WHERE ID IN (?, ?)" );
235
226
verify (preparedStatement ).setObject (1 , 3 );
236
227
}
237
228
@@ -246,14 +237,11 @@ public void testQueryForObjectWithParamMapAndListOfExpressionLists() throws Exce
246
237
l1 .add (new Object [] {3 , "Rod" });
247
238
l1 .add (new Object [] {4 , "Juergen" });
248
239
params .addValue ("multiExpressionList" , l1 );
249
- Object o = template .queryForObject (
250
- "SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN (:multiExpressionList)" ,
240
+ Integer value = template .queryForObject ("SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN (:multiExpressionList)" ,
251
241
params , Integer .class );
252
242
253
- boolean condition = o instanceof Integer ;
254
- assertThat (condition ).as ("Correct result type" ).isTrue ();
255
- verify (connection ).prepareStatement (
256
- "SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN ((?, ?), (?, ?))" );
243
+ assertThat (value ).isEqualTo (22 );
244
+ verify (connection ).prepareStatement ("SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN ((?, ?), (?, ?))" );
257
245
verify (preparedStatement ).setObject (1 , 3 );
258
246
}
259
247
0 commit comments