Skip to content

Commit bfe5aac

Browse files
committed
JMock -> Mockito
1 parent d51490f commit bfe5aac

27 files changed

+415
-1032
lines changed

pom.xml

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,12 @@
169169
<version>10.3.2.1</version>
170170
<scope>test</scope>
171171
</dependency>
172-
<dependency>
173-
<groupId>org.jmock</groupId>
174-
<artifactId>jmock-junit4</artifactId>
175-
<version>2.5.1</version>
172+
<dependency>
173+
<groupId>org.mockito</groupId>
174+
<artifactId>mockito-core</artifactId>
175+
<version>1.9.0</version>
176176
<scope>test</scope>
177-
</dependency>
178-
<dependency>
179-
<groupId>org.jmock</groupId>
180-
<artifactId>jmock-legacy</artifactId>
181-
<version>2.5.1</version>
182-
<scope>test</scope>
183-
<exclusions>
184-
<exclusion>
185-
<groupId>cglib</groupId>
186-
<artifactId>cglib-nodep</artifactId>
187-
</exclusion>
188-
</exclusions>
189-
</dependency>
177+
</dependency>
190178
<dependency>
191179
<groupId>commons-dbcp</groupId>
192180
<artifactId>commons-dbcp</artifactId>

src/test/java/org/apache/ibatis/executor/resultset/FastResultSetHandlerTest.java

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
package org.apache.ibatis.executor.resultset;
1717

18-
import static org.junit.Assert.*;
18+
import static org.junit.Assert.assertEquals;
19+
import static org.mockito.Mockito.when;
1920

2021
import java.sql.Connection;
2122
import java.sql.DatabaseMetaData;
@@ -39,13 +40,24 @@
3940
import org.apache.ibatis.session.ResultHandler;
4041
import org.apache.ibatis.session.RowBounds;
4142
import org.apache.ibatis.type.TypeHandlerRegistry;
42-
import org.jmock.Expectations;
43-
import org.jmock.Mockery;
44-
import org.jmock.integration.junit4.JUnit4Mockery;
4543
import org.junit.Test;
44+
import org.junit.runner.RunWith;
45+
import org.mockito.Mock;
46+
import org.mockito.runners.MockitoJUnitRunner;
4647

48+
@RunWith(MockitoJUnitRunner.class)
4749
public class FastResultSetHandlerTest {
48-
private Mockery context = new JUnit4Mockery();
50+
51+
@Mock
52+
private Statement stmt;
53+
@Mock
54+
private ResultSet rs;
55+
@Mock
56+
private ResultSetMetaData rsmd;
57+
@Mock
58+
private Connection conn;
59+
@Mock
60+
private DatabaseMetaData dbmd;
4961

5062
/**
5163
* Contrary to the spec, some drivers require case-sensitive column names when getting result.
@@ -54,17 +66,19 @@ public class FastResultSetHandlerTest {
5466
*/
5567
@Test
5668
public void shouldRetainColumnNameCase() throws Exception {
69+
5770
final Configuration config = new Configuration();
5871
final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
59-
final MappedStatement ms = new MappedStatement.Builder(config, "testSelect", new StaticSqlSource(config, "some select statement"), SqlCommandType.SELECT).resultMaps(new ArrayList<ResultMap>() {
60-
{
61-
add(new ResultMap.Builder(config, "testMap", HashMap.class, new ArrayList() {
72+
final MappedStatement ms = new MappedStatement.Builder(config, "testSelect", new StaticSqlSource(config, "some select statement"), SqlCommandType.SELECT).resultMaps(
73+
new ArrayList<ResultMap>() {
6274
{
63-
add(new ResultMapping.Builder(config, "cOlUmN1", "CoLuMn1", registry.getTypeHandler(Integer.class)).build());
75+
add(new ResultMap.Builder(config, "testMap", HashMap.class, new ArrayList() {
76+
{
77+
add(new ResultMapping.Builder(config, "cOlUmN1", "CoLuMn1", registry.getTypeHandler(Integer.class)).build());
78+
}
79+
}).build());
6480
}
65-
}).build());
66-
}
67-
}).build();
81+
}).build();
6882

6983
final Executor executor = null;
7084
final ParameterHandler parameterHandler = null;
@@ -73,49 +87,19 @@ public void shouldRetainColumnNameCase() throws Exception {
7387
final RowBounds rowBounds = new RowBounds(0, 100);
7488
final FastResultSetHandler fastResultSetHandler = new FastResultSetHandler(executor, ms, parameterHandler, resultHandler, boundSql, rowBounds);
7589

76-
final Statement stmt = context.mock(Statement.class);
77-
final ResultSet rs = context.mock(ResultSet.class);
78-
final ResultSetMetaData rsmd = context.mock(ResultSetMetaData.class);
79-
final Connection conn = context.mock(Connection.class);
80-
final DatabaseMetaData dbmd = context.mock(DatabaseMetaData.class);
81-
82-
context.checking(new Expectations() {
83-
{
84-
allowing(stmt).getResultSet();
85-
will(returnValue(rs));
86-
87-
allowing(rs).getMetaData();
88-
will(returnValue(rsmd));
89-
allowing(rs).getType();
90-
will(returnValue(ResultSet.TYPE_FORWARD_ONLY));
91-
allowing(rs).close();
92-
oneOf(rs).next();
93-
will(returnValue(true));
94-
oneOf(rs).next();
95-
will(returnValue(false)); // just one row.
96-
allowing(rs).getInt("CoLuMn1");
97-
will(returnValue(100));
98-
allowing(rs).wasNull();
99-
will(returnValue(false));
100-
101-
allowing(rsmd).getColumnCount();
102-
will(returnValue(1));
103-
allowing(rsmd).getColumnLabel(1);
104-
will(returnValue("CoLuMn1"));
105-
allowing(rsmd).getColumnType(1);
106-
will(returnValue(Types.INTEGER));
107-
allowing(rsmd).getColumnClassName(1);
108-
will(returnValue(Integer.class.getCanonicalName()));
109-
allowing(stmt).getConnection();
110-
will(returnValue(conn));
111-
112-
allowing(conn).getMetaData();
113-
will(returnValue(dbmd));
114-
115-
allowing(dbmd).supportsMultipleResultSets();
116-
will(returnValue(false)); // for simplicity.
117-
}
118-
});
90+
when(stmt.getResultSet()).thenReturn(rs);
91+
when(rs.getMetaData()).thenReturn(rsmd);
92+
when(rs.getType()).thenReturn(ResultSet.TYPE_FORWARD_ONLY);
93+
when(rs.next()).thenReturn(true).thenReturn(false);
94+
when(rs.getInt("CoLuMn1")).thenReturn(100);
95+
when(rs.wasNull()).thenReturn(false);
96+
when(rsmd.getColumnCount()).thenReturn(1);
97+
when(rsmd.getColumnLabel(1)).thenReturn("CoLuMn1");
98+
when(rsmd.getColumnType(1)).thenReturn(Types.INTEGER);
99+
when(rsmd.getColumnClassName(1)).thenReturn(Integer.class.getCanonicalName());
100+
when(stmt.getConnection()).thenReturn(conn);
101+
when(conn.getMetaData()).thenReturn(dbmd);
102+
when(dbmd.supportsMultipleResultSets()).thenReturn(false); // for simplicity.
119103

120104
final List<Object> results = fastResultSetHandler.handleResultSets(stmt);
121105
assertEquals(1, results.size());

src/test/java/org/apache/ibatis/transaction/managed/ManagedTransactionFactoryTest.java

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,65 +15,47 @@
1515
*/
1616
package org.apache.ibatis.transaction.managed;
1717

18-
import org.apache.ibatis.BaseDataTest;
19-
import org.apache.ibatis.transaction.Transaction;
20-
import org.apache.ibatis.transaction.TransactionFactory;
21-
import org.jmock.Expectations;
22-
import org.jmock.Mockery;
23-
import org.jmock.lib.legacy.ClassImposteriser;
2418
import static org.junit.Assert.assertEquals;
25-
import org.junit.Test;
19+
import static org.mockito.Mockito.verify;
20+
import static org.mockito.Mockito.verifyNoMoreInteractions;
2621

2722
import java.sql.Connection;
2823
import java.util.Properties;
2924

30-
public class ManagedTransactionFactoryTest extends BaseDataTest {
25+
import org.apache.ibatis.BaseDataTest;
26+
import org.apache.ibatis.transaction.Transaction;
27+
import org.apache.ibatis.transaction.TransactionFactory;
28+
import org.junit.Test;
29+
import org.mockito.Mockito;
3130

32-
protected Mockery mockery = new Mockery() {
33-
{
34-
setImposteriser(ClassImposteriser.INSTANCE);
35-
}
36-
};
31+
public class ManagedTransactionFactoryTest extends BaseDataTest {
3732

38-
protected final Connection conn = mockery.mock(Connection.class);
33+
protected final Connection conn = Mockito.mock(Connection.class);
3934

4035
@Test
4136
public void shouldEnsureThatCallsToManagedTransactionAPIDoNotForwardToManagedConnections() throws Exception {
42-
mockery.checking(new Expectations() {
43-
{
44-
one(conn).close();
45-
}
46-
});
47-
4837
TransactionFactory tf = new ManagedTransactionFactory();
4938
tf.setProperties(new Properties());
5039
Transaction tx = tf.newTransaction(conn);
5140
assertEquals(conn, tx.getConnection());
5241
tx.commit();
5342
tx.rollback();
5443
tx.close();
55-
mockery.assertIsSatisfied();
44+
verify(conn).close();
5645
}
5746

58-
5947
@Test
6048
public void shouldEnsureThatCallsToManagedTransactionAPIDoNotForwardToManagedConnectionsAndDoesNotCloseConnection() throws Exception {
61-
mockery.checking(new Expectations() {
62-
{
63-
never(conn).close();
64-
}
65-
});
66-
6749
TransactionFactory tf = new ManagedTransactionFactory();
6850
Properties props = new Properties();
69-
props.setProperty("closeConnection","false");
51+
props.setProperty("closeConnection", "false");
7052
tf.setProperties(props);
7153
Transaction tx = tf.newTransaction(conn);
7254
assertEquals(conn, tx.getConnection());
7355
tx.commit();
7456
tx.rollback();
7557
tx.close();
76-
mockery.assertIsSatisfied();
58+
verifyNoMoreInteractions(conn);
7759
}
7860

7961
}

src/test/java/org/apache/ibatis/type/BaseTypeHandlerTest.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,31 @@
1515
*/
1616
package org.apache.ibatis.type;
1717

18-
import org.jmock.Mockery;
19-
import org.jmock.lib.legacy.ClassImposteriser;
18+
import java.sql.CallableStatement;
19+
import java.sql.PreparedStatement;
20+
import java.sql.ResultSet;
21+
import java.sql.ResultSetMetaData;
2022

21-
import java.sql.*;
23+
import org.junit.runner.RunWith;
24+
import org.mockito.Mock;
25+
import org.mockito.runners.MockitoJUnitRunner;
2226

27+
@RunWith(MockitoJUnitRunner.class)
2328
public abstract class BaseTypeHandlerTest {
2429

25-
protected Mockery mockery = new Mockery() {
26-
{
27-
setImposteriser(ClassImposteriser.INSTANCE);
28-
}
29-
};
30+
@Mock
31+
protected ResultSet rs;
32+
@Mock
33+
protected PreparedStatement ps;
34+
@Mock
35+
protected CallableStatement cs;
36+
@Mock
37+
protected ResultSetMetaData rsmd;
3038

31-
protected final ResultSet rs = mockery.mock(ResultSet.class);
32-
protected final PreparedStatement ps = mockery.mock(PreparedStatement.class);
33-
protected final CallableStatement cs = mockery.mock(CallableStatement.class);
34-
protected final ResultSetMetaData rsmd = mockery.mock(ResultSetMetaData.class);
39+
public abstract void shouldSetParameter() throws Exception;
3540

36-
public abstract void shouldSetParameter()
37-
throws Exception;
41+
public abstract void shouldGetResultFromResultSet() throws Exception;
3842

39-
public abstract void shouldGetResultFromResultSet()
40-
throws Exception;
41-
42-
public abstract void shouldGetResultFromCallableStatement()
43-
throws Exception;
43+
public abstract void shouldGetResultFromCallableStatement() throws Exception;
4444

4545
}

src/test/java/org/apache/ibatis/type/BigDecimalTypeHandlerTest.java

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,36 @@
1515
*/
1616
package org.apache.ibatis.type;
1717

18-
import org.jmock.Expectations;
1918
import static org.junit.Assert.assertEquals;
20-
import org.junit.Test;
19+
import static org.mockito.Mockito.verify;
20+
import static org.mockito.Mockito.when;
2121

2222
import java.math.BigDecimal;
2323

24+
import org.junit.Test;
25+
2426
public class BigDecimalTypeHandlerTest extends BaseTypeHandlerTest {
2527

26-
private static final TypeHandler TYPE_HANDLER = new BigDecimalTypeHandler();
28+
private static final TypeHandler<BigDecimal> TYPE_HANDLER = new BigDecimalTypeHandler();
2729

2830
@Test
29-
public void shouldSetParameter()
30-
throws Exception {
31-
mockery.checking(new Expectations() {
32-
{
33-
one(ps).setBigDecimal(with(any(int.class)), with(any(BigDecimal.class)));
34-
}
35-
});
31+
public void shouldSetParameter() throws Exception {
3632
TYPE_HANDLER.setParameter(ps, 1, new BigDecimal(1), null);
37-
mockery.assertIsSatisfied();
33+
verify(ps).setBigDecimal(1, new BigDecimal(1));
3834
}
3935

4036
@Test
41-
public void shouldGetResultFromResultSet()
42-
throws Exception {
43-
mockery.checking(new Expectations() {
44-
{
45-
one(rs).getBigDecimal(with(any(String.class)));
46-
will(returnValue(new BigDecimal(1)));
47-
one(rs).wasNull();
48-
will(returnValue(false));
49-
}
50-
});
37+
public void shouldGetResultFromResultSet() throws Exception {
38+
when(rs.getBigDecimal("column")).thenReturn(new BigDecimal(1));
39+
when(rs.wasNull()).thenReturn(false);
5140
assertEquals(new BigDecimal(1), TYPE_HANDLER.getResult(rs, "column"));
52-
mockery.assertIsSatisfied();
5341
}
5442

5543
@Test
56-
public void shouldGetResultFromCallableStatement()
57-
throws Exception {
58-
mockery.checking(new Expectations() {
59-
{
60-
one(cs).getBigDecimal(with(any(int.class)));
61-
will(returnValue(new BigDecimal(1)));
62-
one(cs).wasNull();
63-
will(returnValue(false));
64-
}
65-
});
44+
public void shouldGetResultFromCallableStatement() throws Exception {
45+
when(cs.getBigDecimal(1)).thenReturn(new BigDecimal(1));
46+
when(cs.wasNull()).thenReturn(false);
6647
assertEquals(new BigDecimal(1), TYPE_HANDLER.getResult(cs, 1));
67-
mockery.assertIsSatisfied();
6848
}
6949

70-
7150
}

0 commit comments

Comments
 (0)