1414
1515package org .jdbi .v3 ;
1616
17- import static org .easymock .EasyMock .expect ;
18- import static org .easymock .EasyMock .replay ;
1917import static org .junit .Assert .assertEquals ;
2018import static org .junit .Assert .assertNotNull ;
2119import static org .junit .Assert .assertNull ;
20+ import static org .mockito .Mockito .when ;
2221
2322import java .math .BigDecimal ;
2423import java .sql .ResultSet ;
2524import java .sql .ResultSetMetaData ;
25+ import java .sql .SQLException ;
2626import java .util .Collections ;
2727
28- import org .easymock . EasyMockRunner ;
29- import org .easymock . Mock ;
28+ import org .junit . Before ;
29+ import org .junit . Rule ;
3030import org .junit .Test ;
31- import org .junit .runner .RunWith ;
31+ import org .mockito .Mock ;
32+ import org .mockito .junit .MockitoJUnit ;
33+ import org .mockito .junit .MockitoRule ;
3234
33- @ RunWith (EasyMockRunner .class )
3435public class BeanMapperTest {
36+
37+ @ Rule
38+ public MockitoRule mockitoRule = MockitoJUnit .rule ();
3539
3640 @ Mock
3741 ResultSet resultSet ;
@@ -41,19 +45,21 @@ public class BeanMapperTest {
4145
4246 TestingStatementContext ctx = new TestingStatementContext (Collections .emptyMap ());
4347
44- BeanMapper <SampleBean > mapper = new BeanMapper <SampleBean >(SampleBean .class );
48+ BeanMapper <SampleBean > mapper = new BeanMapper <>(SampleBean .class );
49+
50+ @ Before
51+ public void setUpMocks () throws SQLException {
52+ when (resultSet .getMetaData ()).thenReturn (resultSetMetaData );
53+ }
4554
4655 @ Test
4756 public void shouldSetValueOnPublicSetter () throws Exception {
48- expect (resultSetMetaData .getColumnCount ()).andReturn (1 ).anyTimes ();
49- expect (resultSetMetaData .getColumnLabel (1 )).andReturn ("longField" );
50- replay (resultSetMetaData );
57+ when (resultSetMetaData .getColumnCount ()).thenReturn (1 );
58+ when (resultSetMetaData .getColumnLabel (1 )).thenReturn ("longField" );
5159
52- expect (resultSet .getMetaData ()).andReturn (resultSetMetaData );
5360 Long aLongVal = 100l ;
54- expect (resultSet .getLong (1 )).andReturn (aLongVal );
55- expect (resultSet .wasNull ()).andReturn (false );
56- replay (resultSet );
61+ when (resultSet .getLong (1 )).thenReturn (aLongVal );
62+ when (resultSet .wasNull ()).thenReturn (false );
5763
5864 SampleBean sampleBean = mapper .map (0 , resultSet , ctx );
5965
@@ -62,15 +68,12 @@ public void shouldSetValueOnPublicSetter() throws Exception {
6268
6369 @ Test
6470 public void shouldHandleColumNameWithUnderscores () throws Exception {
65- expect (resultSetMetaData .getColumnCount ()).andReturn (1 ).anyTimes ();
66- expect (resultSetMetaData .getColumnLabel (1 )).andReturn ("LONG_FIELD" );
67- replay (resultSetMetaData );
71+ when (resultSetMetaData .getColumnCount ()).thenReturn (1 );
72+ when (resultSetMetaData .getColumnLabel (1 )).thenReturn ("LONG_FIELD" );
6873
69- expect (resultSet .getMetaData ()).andReturn (resultSetMetaData );
7074 Long aLongVal = 100l ;
71- expect (resultSet .getLong (1 )).andReturn (aLongVal );
72- expect (resultSet .wasNull ()).andReturn (false );
73- replay (resultSet );
75+ when (resultSet .getLong (1 )).thenReturn (aLongVal );
76+ when (resultSet .wasNull ()).thenReturn (false );
7477
7578 SampleBean sampleBean = mapper .map (0 , resultSet , ctx );
7679
@@ -79,15 +82,12 @@ public void shouldHandleColumNameWithUnderscores() throws Exception {
7982
8083 @ Test
8184 public void shouldBeCaseInSensitiveOfColumnWithUnderscoresAndPropertyNames () throws Exception {
82- expect (resultSetMetaData .getColumnCount ()).andReturn (1 ).anyTimes ();
83- expect (resultSetMetaData .getColumnLabel (1 )).andReturn ("LoNg_FiElD" );
84- replay (resultSetMetaData );
85+ when (resultSetMetaData .getColumnCount ()).thenReturn (1 );
86+ when (resultSetMetaData .getColumnLabel (1 )).thenReturn ("LoNg_FiElD" );
8587
86- expect (resultSet .getMetaData ()).andReturn (resultSetMetaData );
8788 Long aLongVal = 100l ;
88- expect (resultSet .getLong (1 )).andReturn (aLongVal );
89- expect (resultSet .wasNull ()).andReturn (false );
90- replay (resultSet );
89+ when (resultSet .getLong (1 )).thenReturn (aLongVal );
90+ when (resultSet .wasNull ()).thenReturn (false );
9191
9292 SampleBean sampleBean = mapper .map (0 , resultSet , ctx );
9393
@@ -96,11 +96,7 @@ public void shouldBeCaseInSensitiveOfColumnWithUnderscoresAndPropertyNames() thr
9696
9797 @ Test
9898 public void shouldHandleEmptyResult () throws Exception {
99- expect (resultSetMetaData .getColumnCount ()).andReturn (0 );
100- replay (resultSetMetaData );
101-
102- expect (resultSet .getMetaData ()).andReturn (resultSetMetaData );
103- replay (resultSet );
99+ when (resultSetMetaData .getColumnCount ()).thenReturn (0 );
104100
105101 SampleBean sampleBean = mapper .map (0 , resultSet , ctx );
106102
@@ -109,15 +105,12 @@ public void shouldHandleEmptyResult() throws Exception {
109105
110106 @ Test
111107 public void shouldBeCaseInSensitiveOfColumnAndPropertyNames () throws Exception {
112- expect (resultSetMetaData .getColumnCount ()).andReturn (1 ).anyTimes ();
113- expect (resultSetMetaData .getColumnLabel (1 )).andReturn ("LoNgfielD" );
114- replay (resultSetMetaData );
108+ when (resultSetMetaData .getColumnCount ()).thenReturn (1 );
109+ when (resultSetMetaData .getColumnLabel (1 )).thenReturn ("LoNgfielD" );
115110
116- expect (resultSet .getMetaData ()).andReturn (resultSetMetaData );
117111 Long aLongVal = 100l ;
118- expect (resultSet .getLong (1 )).andReturn (aLongVal );
119- expect (resultSet .wasNull ()).andReturn (false );
120- replay (resultSet );
112+ when (resultSet .getLong (1 )).thenReturn (aLongVal );
113+ when (resultSet .wasNull ()).thenReturn (false );
121114 SampleBean sampleBean = mapper .map (0 , resultSet , ctx );
122115
123116 assertEquals (aLongVal , sampleBean .getLongField ());
@@ -126,14 +119,11 @@ public void shouldBeCaseInSensitiveOfColumnAndPropertyNames() throws Exception {
126119
127120 @ Test
128121 public void shouldHandleNullValue () throws Exception {
129- expect (resultSetMetaData .getColumnCount ()).andReturn (1 ).anyTimes ();
130- expect (resultSetMetaData .getColumnLabel (1 )).andReturn ("LoNgfielD" );
131- replay (resultSetMetaData );
122+ when (resultSetMetaData .getColumnCount ()).thenReturn (1 );
123+ when (resultSetMetaData .getColumnLabel (1 )).thenReturn ("LoNgfielD" );
132124
133- expect (resultSet .getMetaData ()).andReturn (resultSetMetaData );
134- expect (resultSet .getLong (1 )).andReturn (0l );
135- expect (resultSet .wasNull ()).andReturn (true );
136- replay (resultSet );
125+ when (resultSet .getLong (1 )).thenReturn (0l );
126+ when (resultSet .wasNull ()).thenReturn (true );
137127
138128 SampleBean sampleBean = mapper .map (0 , resultSet , ctx );
139129
@@ -143,15 +133,12 @@ public void shouldHandleNullValue() throws Exception {
143133
144134 @ Test
145135 public void shouldSetValuesOnPublicSetter () throws Exception {
146- expect (resultSetMetaData .getColumnCount ()).andReturn (1 ).anyTimes ();
147- expect (resultSetMetaData .getColumnLabel (1 )).andReturn ("longField" );
148- replay (resultSetMetaData );
136+ when (resultSetMetaData .getColumnCount ()).thenReturn (1 );
137+ when (resultSetMetaData .getColumnLabel (1 )).thenReturn ("longField" );
149138
150- expect (resultSet .getMetaData ()).andReturn (resultSetMetaData );
151139 Long expected = 1L ;
152- expect (resultSet .getLong (1 )).andReturn (expected );
153- expect (resultSet .wasNull ()).andReturn (false ).anyTimes ();
154- replay (resultSet );
140+ when (resultSet .getLong (1 )).thenReturn (expected );
141+ when (resultSet .wasNull ()).thenReturn (false );
155142
156143 SampleBean sampleBean = mapper .map (0 , resultSet , ctx );
157144
@@ -160,64 +147,52 @@ public void shouldSetValuesOnPublicSetter() throws Exception {
160147
161148 @ Test (expected = IllegalArgumentException .class )
162149 public void shouldThrowOnProtectedSetter () throws Exception {
163- expect (resultSetMetaData .getColumnCount ()).andReturn (1 ).anyTimes ();
164- expect (resultSetMetaData .getColumnLabel (1 )).andReturn ("protectedStringField" );
165- replay (resultSetMetaData );
150+ when (resultSetMetaData .getColumnCount ()).thenReturn (1 );
151+ when (resultSetMetaData .getColumnLabel (1 )).thenReturn ("protectedStringField" );
166152
167- expect (resultSet .getMetaData ()).andReturn (resultSetMetaData );
168153 String expected = "string" ;
169- expect (resultSet .getString (1 )).andReturn (expected );
170- expect (resultSet .wasNull ()).andReturn (false ).anyTimes ();
171- replay (resultSet );
154+ when (resultSet .getString (1 )).thenReturn (expected );
155+ when (resultSet .wasNull ()).thenReturn (false );
172156
173157 mapper .map (0 , resultSet , ctx );
174158 }
175159
176160 @ Test (expected = IllegalArgumentException .class )
177161 public void shouldThrowOnPackagePrivateSetter () throws Exception {
178- expect (resultSetMetaData .getColumnCount ()).andReturn (1 ).anyTimes ();
179- expect (resultSetMetaData .getColumnLabel (1 )).andReturn ("packagePrivateIntField" );
180- replay (resultSetMetaData );
162+ when (resultSetMetaData .getColumnCount ()).thenReturn (1 );
163+ when (resultSetMetaData .getColumnLabel (1 )).thenReturn ("packagePrivateIntField" );
181164
182- expect (resultSet .getMetaData ()).andReturn (resultSetMetaData );
183165 int expected = 200 ;
184- expect (resultSet .getInt (1 )).andReturn (expected );
185- expect (resultSet .wasNull ()).andReturn (false ).anyTimes ();
186- replay (resultSet );
166+ when (resultSet .getInt (1 )).thenReturn (expected );
167+ when (resultSet .wasNull ()).thenReturn (false );
187168
188169 mapper .map (0 , resultSet , ctx );
189170 }
190171
191172 @ Test (expected = IllegalArgumentException .class )
192173 public void shouldThrowOnPrivateSetter () throws Exception {
193- expect (resultSetMetaData .getColumnCount ()).andReturn (1 ).anyTimes ();
194- expect (resultSetMetaData .getColumnLabel (1 )).andReturn ("privateBigDecimalField" );
195- replay (resultSetMetaData );
174+ when (resultSetMetaData .getColumnCount ()).thenReturn (1 );
175+ when (resultSetMetaData .getColumnLabel (1 )).thenReturn ("privateBigDecimalField" );
196176
197- expect (resultSet .getMetaData ()).andReturn (resultSetMetaData );
198177 BigDecimal expected = BigDecimal .ONE ;
199- expect (resultSet .getBigDecimal (1 )).andReturn (expected );
200- expect (resultSet .wasNull ()).andReturn (false ).anyTimes ();
201- replay (resultSet );
178+ when (resultSet .getBigDecimal (1 )).thenReturn (expected );
179+ when (resultSet .wasNull ()).thenReturn (false );
202180
203181 mapper .map (0 , resultSet , ctx );
204182 }
205183
206184 @ Test
207185 public void shouldSetValuesInSuperClassProperties () throws Exception {
208- expect (resultSetMetaData .getColumnCount ()).andReturn (2 ).anyTimes ();
209- expect (resultSetMetaData .getColumnLabel (1 )).andReturn ("longField" );
210- expect (resultSetMetaData .getColumnLabel (2 )).andReturn ("blongField" );
211- replay (resultSetMetaData );
186+ when (resultSetMetaData .getColumnCount ()).thenReturn (2 );
187+ when (resultSetMetaData .getColumnLabel (1 )).thenReturn ("longField" );
188+ when (resultSetMetaData .getColumnLabel (2 )).thenReturn ("blongField" );
212189
213- expect (resultSet .getMetaData ()).andReturn (resultSetMetaData );
214190 Long aLongVal = 100l ;
215191 Long bLongVal = 200l ;
216192
217- expect (resultSet .getLong (1 )).andReturn (aLongVal );
218- expect (resultSet .getLong (2 )).andReturn (bLongVal );
219- expect (resultSet .wasNull ()).andReturn (false ).anyTimes ();
220- replay (resultSet );
193+ when (resultSet .getLong (1 )).thenReturn (aLongVal );
194+ when (resultSet .getLong (2 )).thenReturn (bLongVal );
195+ when (resultSet .wasNull ()).thenReturn (false );
221196
222197 BeanMapper <DerivedBean > mapper = new BeanMapper <DerivedBean >(DerivedBean .class );
223198
@@ -231,16 +206,13 @@ public void shouldSetValuesInSuperClassProperties() throws Exception {
231206 public void shouldUseRegisteredMapperForUnknownPropertyType () throws Exception {
232207 ctx .registerColumnMapper (new ValueTypeMapper ());
233208
234- expect (resultSetMetaData .getColumnCount ()).andReturn (2 ).anyTimes ();
235- expect (resultSetMetaData .getColumnLabel (1 )).andReturn ("longField" );
236- expect (resultSetMetaData .getColumnLabel (2 )).andReturn ("valueTypeField" ).anyTimes ();
237- replay (resultSetMetaData );
209+ when (resultSetMetaData .getColumnCount ()).thenReturn (2 );
210+ when (resultSetMetaData .getColumnLabel (1 )).thenReturn ("longField" );
211+ when (resultSetMetaData .getColumnLabel (2 )).thenReturn ("valueTypeField" );
238212
239- expect (resultSet .getMetaData ()).andReturn (resultSetMetaData ).anyTimes ();
240- expect (resultSet .getLong (1 )).andReturn (123L );
241- expect (resultSet .getString (2 )).andReturn ("foo" );
242- expect (resultSet .wasNull ()).andReturn (false ).anyTimes ();
243- replay (resultSet );
213+ when (resultSet .getLong (1 )).thenReturn (123L );
214+ when (resultSet .getString (2 )).thenReturn ("foo" );
215+ when (resultSet .wasNull ()).thenReturn (false );
244216
245217 SampleBean sampleBean = mapper .map (0 , resultSet , ctx );
246218
@@ -251,16 +223,13 @@ public void shouldUseRegisteredMapperForUnknownPropertyType() throws Exception {
251223
252224 @ Test (expected = IllegalArgumentException .class )
253225 public void shouldThrowOnPropertyTypeWithoutRegisteredMapper () throws Exception {
254- expect (resultSetMetaData .getColumnCount ()).andReturn (2 ).anyTimes ();
255- expect (resultSetMetaData .getColumnLabel (1 )).andReturn ("longField" );
256- expect (resultSetMetaData .getColumnLabel (2 )).andReturn ("valueTypeField" ).anyTimes ();
257- replay (resultSetMetaData );
258-
259- expect (resultSet .getMetaData ()).andReturn (resultSetMetaData ).anyTimes ();
260- expect (resultSet .getLong (1 )).andReturn (123L );
261- expect (resultSet .getObject (2 )).andReturn (new Object ());
262- expect (resultSet .wasNull ()).andReturn (false ).anyTimes ();
263- replay (resultSet );
226+ when (resultSetMetaData .getColumnCount ()).thenReturn (2 );
227+ when (resultSetMetaData .getColumnLabel (1 )).thenReturn ("longField" );
228+ when (resultSetMetaData .getColumnLabel (2 )).thenReturn ("valueTypeField" );
229+
230+ when (resultSet .getLong (1 )).thenReturn (123L );
231+ when (resultSet .getObject (2 )).thenReturn (new Object ());
232+ when (resultSet .wasNull ()).thenReturn (false );
264233
265234 mapper .map (0 , resultSet , ctx );
266235 }
0 commit comments