Skip to content

Commit 86a9c2b

Browse files
committed
Switched to Mockito to improve readability of tests.
1 parent 4b1e61b commit 86a9c2b

3 files changed

Lines changed: 63 additions & 102 deletions

File tree

‎pom.xml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
<scope>test</scope>
1616
</dependency>
1717
<dependency>
18-
<groupId>org.easymock</groupId>
19-
<artifactId>easymock</artifactId>
20-
<version>3.1</version>
18+
<groupId>org.mockito</groupId>
19+
<artifactId>mockito-all</artifactId>
20+
<version>1.9.0</version>
2121
<scope>test</scope>
2222
</dependency>
2323
</dependencies>

‎src/test/java/com/blogspot/ihaztehcodez/magic/TestGlue.java‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,53 @@
11
package com.blogspot.ihaztehcodez.magic;
22

3+
import org.junit.Test;
4+
35
import java.lang.reflect.InvocationHandler;
46
import java.lang.reflect.Proxy;
57
import java.sql.SQLException;
68

7-
import org.junit.Test;
89
import static junit.framework.Assert.*;
9-
import static org.easymock.EasyMock.*;
10+
import static org.mockito.Mockito.mock;
1011

1112
public class TestGlue {
1213
@Test( expected = IllegalArgumentException.class)
1314
public void toDatabasePackage_validatesPassedClassIsInterface() {
14-
Glue glue = new Glue(createNiceMock(InvocationHandler.class));
15+
Glue glue = new Glue(mock(InvocationHandler.class));
1516
glue.toDatabasePackage(TestGlue.class);
1617
}
1718

1819
@Test( expected = IllegalArgumentException.class)
1920
public void toDatabasePackage_validatesPassedInterfaceHasNoMethods() {
20-
Glue glue = new Glue(createNiceMock(InvocationHandler.class));
21+
Glue glue = new Glue(mock(InvocationHandler.class));
2122
glue.toDatabasePackage(NoMethods.class);
2223
}
2324
@Test( expected = IllegalArgumentException.class)
2425
public void toDatabasePackage_validatesPassedInterfaceDoesNotExtendsAnything() {
25-
Glue glue = new Glue(createNiceMock(InvocationHandler.class));
26+
Glue glue = new Glue(mock(InvocationHandler.class));
2627
glue.toDatabasePackage(ExtendsFine.class);
2728
}
2829

2930
@Test( expected = IllegalArgumentException.class)
3031
public void toDatabasePackage_validatesPassedInterfaceHasMethodsWithoutAnnotation() {
31-
Glue glue = new Glue(createNiceMock(InvocationHandler.class));
32+
Glue glue = new Glue(mock(InvocationHandler.class));
3233
glue.toDatabasePackage(OneMethodsWithAnnotation.class);
3334
}
3435

3536
@Test( expected = IllegalArgumentException.class)
3637
public void toDatabasePackage_validatesAllMethodsWithoutAnnotation() {
37-
Glue glue = new Glue(createNiceMock(InvocationHandler.class));
38+
Glue glue = new Glue(mock(InvocationHandler.class));
3839
glue.toDatabasePackage(NoMethodsWithAnnotation.class);
3940
}
4041

4142
@Test( expected = IllegalArgumentException.class)
4243
public void toDatabasePackage_validatesMethodsThrowSQLException() {
43-
Glue glue = new Glue(createNiceMock(InvocationHandler.class));
44+
Glue glue = new Glue(mock(InvocationHandler.class));
4445
glue.toDatabasePackage(DoesNotThrow.class);
4546
}
4647

4748
@Test
4849
public void toDatabasePackage_returnsProxyWhenGivenAValidInterface() {
49-
InvocationHandler handler = createNiceMock(InvocationHandler.class);
50+
InvocationHandler handler = mock(InvocationHandler.class);
5051
Glue glue = new Glue(handler);
5152

5253
Fine fine = glue.toDatabasePackage(Fine.class);

‎src/test/java/com/blogspot/ihaztehcodez/magic/TestSQLHandler.java‎

Lines changed: 50 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package com.blogspot.ihaztehcodez.magic;
22

3+
import com.blogspot.ihaztehcodez.magic.binders.VoidBinding;
4+
import com.blogspot.ihaztehcodez.magic.utility.SimpleConnectionProvider;
5+
import org.junit.Test;
6+
37
import java.sql.CallableStatement;
48
import java.sql.Connection;
59
import java.sql.SQLException;
610
import java.util.ArrayList;
711
import java.util.LinkedList;
812
import java.util.List;
913

10-
import org.junit.Test;
11-
12-
import com.blogspot.ihaztehcodez.magic.utility.SimpleConnectionProvider;
13-
14-
import static junit.framework.Assert.*;
15-
import static org.easymock.EasyMock.*;
16-
14+
import static junit.framework.Assert.assertNull;
15+
import static junit.framework.Assert.fail;
16+
import static org.mockito.Mockito.*;
17+
1718
public class TestSQLHandler {
1819
@Test( expected = NullPointerException.class)
1920
public void ctor_rejectsNullDatabase() {
@@ -22,117 +23,77 @@ public void ctor_rejectsNullDatabase() {
2223

2324
@Test
2425
public void invoke_executesStatementGivenInAnnotation() throws Throwable {
25-
Connection connection = createMock(Connection.class);
26-
CallableStatement statement = createMock(CallableStatement.class);
27-
28-
expect(connection.prepareCall("SQL"))
29-
.andReturn(statement);
30-
expect(statement.execute()).andReturn(false);
26+
Connection connection = mock(Connection.class);
27+
CallableStatement statement = mock(CallableStatement.class);
3128

32-
statement.close();
33-
connection.close();
34-
35-
Binding returnBinding = createMock(Binding.class);
36-
expect(returnBinding.worksWith()).andReturn(new Class<?>[] { void.class } );
37-
returnBinding.prepareGet(statement, 1);
38-
expect(returnBinding.getValue(statement, 1)).andReturn(null);
39-
expect(returnBinding.parameters()).andReturn(0);
29+
when(connection.prepareCall("SQL"))
30+
.thenReturn(statement);
31+
32+
Binding returnBinding = new VoidBinding();
4033
List<Binding> bindings = new LinkedList<Binding>();
4134
bindings.add(returnBinding);
42-
43-
replay(connection, statement, returnBinding);
44-
35+
4536
SQLHandler subject = new SQLHandler(bindings, new SimpleConnectionProvider(connection));
4637
assertNull(subject.invoke(null, Stubby.class.getMethod("stubby"), new Object[0]));
47-
48-
verify(connection, statement, returnBinding);
38+
39+
verify(statement).execute();
4940
}
5041

5142
@Test
5243
public void invoke_passesOnVariables() throws Throwable {
53-
Connection connection = createMock(Connection.class);
54-
CallableStatement statement = createMock(CallableStatement.class);
55-
56-
expect(connection.prepareCall("SQL"))
57-
.andReturn(statement);
58-
expect(statement.execute()).andReturn(false);
59-
60-
statement.close();
61-
connection.close();
62-
44+
Connection connection = mock(Connection.class);
45+
CallableStatement statement = mock(CallableStatement.class);
46+
47+
when(connection.prepareCall("SQL"))
48+
.thenReturn(statement);
49+
6350
List<Binding> bindings = new LinkedList<Binding>();
6451

65-
Binding returnBinding = createMock(Binding.class);
66-
expect(returnBinding.worksWith()).andReturn(new Class<?>[] { void.class } );
67-
returnBinding.prepareGet(statement, 1);
68-
expect(returnBinding.parameters()).andReturn(0);
69-
expect(returnBinding.getValue(statement, 1)).andReturn(null);
52+
Binding returnBinding = new VoidBinding();
7053
bindings.add(returnBinding);
7154

72-
Binding paramiter = createMock(Binding.class);
73-
expect(paramiter.worksWith()).andReturn(new Class<?>[] { int.class } );
74-
paramiter.setValue(statement, 1, 8);
75-
expect(paramiter.parameters()).andReturn(1);
76-
bindings.add(paramiter);
77-
78-
replay(connection, statement, returnBinding, paramiter);
55+
Binding paramiter = mock(Binding.class);
56+
when(paramiter.worksWith()).thenReturn(new Class<?>[]{int.class});
7957

58+
when(paramiter.parameters()).thenReturn(1);
59+
bindings.add(paramiter);
60+
8061
SQLHandler subject = new SQLHandler(bindings, new SimpleConnectionProvider(connection));
81-
assertNull(subject.invoke(null, Stubby.class.getMethod("intIt", new Class<?>[] { int.class } ),
62+
assertNull(subject.invoke(null, Stubby.class.getMethod("intIt", int.class),
8263
new Object[] { 8 }));
83-
84-
verify(connection, statement, returnBinding, paramiter);
64+
65+
verify(paramiter).setValue(statement, 1, 8);
8566
}
8667

8768
@Test
8869
public void invoke_commitsOnRequest() throws Throwable {
89-
Connection connection = createMock(Connection.class);
90-
CallableStatement statement = createMock(CallableStatement.class);
91-
92-
expect(connection.prepareCall("SQL"))
93-
.andReturn(statement);
94-
expect(statement.execute()).andReturn(false);
95-
connection.commit();
96-
statement.close();
97-
connection.close();
70+
Connection connection = mock(Connection.class);
71+
CallableStatement statement = mock(CallableStatement.class);
72+
73+
when(connection.prepareCall("SQL"))
74+
.thenReturn(statement);
9875

99-
Binding returnBinding = createMock(Binding.class);
100-
expect(returnBinding.worksWith()).andReturn(new Class<?>[] { void.class } );
101-
returnBinding.prepareGet(statement, 1);
102-
expect(returnBinding.getValue(statement, 1)).andReturn(null);
103-
expect(returnBinding.parameters()).andReturn(0);
10476
List<Binding> bindings = new LinkedList<Binding>();
105-
bindings.add(returnBinding);
106-
107-
replay(connection, statement, returnBinding);
108-
77+
bindings.add(new VoidBinding());
78+
10979
SQLHandler subject = new SQLHandler(bindings, new SimpleConnectionProvider(connection));
11080
assertNull(subject.invoke(null, Stubby.class.getMethod("commiting"), new Object[0]));
11181

112-
verify(connection, statement, returnBinding);
82+
verify(connection).commit();
11383
}
11484

11585
@Test
11686
public void invoke_rollsbackOnRequest() throws Throwable {
117-
Connection connection = createMock(Connection.class);
118-
CallableStatement statement = createMock(CallableStatement.class);
119-
120-
expect(connection.prepareCall("SQL"))
121-
.andReturn(statement);
122-
expect(statement.execute()).andThrow(new SQLException());
123-
connection.rollback();
124-
statement.close();
125-
connection.close();
126-
127-
Binding returnBinding = createMock(Binding.class);
128-
expect(returnBinding.worksWith()).andReturn(new Class<?>[] { void.class } );
129-
returnBinding.prepareGet(statement, 1);
130-
expect(returnBinding.parameters()).andReturn(0);
87+
Connection connection = mock(Connection.class);
88+
CallableStatement statement = mock(CallableStatement.class);
89+
90+
when(connection.prepareCall("SQL"))
91+
.thenReturn(statement);
92+
when(statement.execute()).thenThrow(new SQLException());
93+
13194
List<Binding> bindings = new LinkedList<Binding>();
132-
bindings.add(returnBinding);
133-
134-
replay(connection, statement, returnBinding);
135-
95+
bindings.add(new VoidBinding());
96+
13697
SQLHandler subject = new SQLHandler(bindings, new SimpleConnectionProvider(connection));
13798
try {
13899
assertNull(subject.invoke(null, Stubby.class.getMethod("commiting"), new Object[0]));
@@ -141,9 +102,8 @@ public void invoke_rollsbackOnRequest() throws Throwable {
141102
// Expected.
142103
}
143104

144-
verify(connection, statement, returnBinding);
105+
verify(connection).rollback();
145106
}
146-
147107
}
148108

149109
interface Stubby {

0 commit comments

Comments
 (0)