Skip to content

Commit 330457b

Browse files
committed
Merge branch 'SPR-10126' into cleanup-3.2.x
* SPR-10126: Replace EasyMock with Mockito in test sources
2 parents cbf6991 + d66c733 commit 330457b

File tree

82 files changed

+4827
-10459
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+4827
-10459
lines changed

build.gradle

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ configure(allprojects) {
7474
dependencies {
7575
testCompile("junit:junit:${junitVersion}")
7676
testCompile("org.hamcrest:hamcrest-all:1.3")
77-
testCompile("org.easymock:easymock:${easymockVersion}")
77+
testCompile("org.mockito:mockito-core:1.9.5")
7878
}
7979

8080
ext.javadocLinks = [
@@ -100,6 +100,16 @@ configure(allprojects) {
100100
] as String[]
101101
}
102102

103+
configure(allprojects.findAll{it.name in ["spring", "spring-jms", "spring-orm",
104+
"spring-orm-hibernate4", "spring-oxm", "spring-struts", "spring-test",
105+
"spring-test-mvc", "spring-tx", "spring-web", "spring-webmvc",
106+
"spring-webmvc-portlet", "spring-webmvc-tiles3"]}) {
107+
dependencies {
108+
testCompile("org.easymock:easymock:${easymockVersion}")
109+
testCompile "org.easymock:easymockclassextension:${easymockVersion}"
110+
}
111+
}
112+
103113
configure(subprojects) { subproject ->
104114
apply plugin: "merge"
105115
apply from: "${gradleScriptDir}/publish-maven.gradle"
@@ -330,7 +340,6 @@ project("spring-tx") {
330340
optional("javax.resource:connector-api:1.5")
331341
optional("org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1")
332342
optional("javax.ejb:ejb-api:3.0")
333-
testCompile "org.easymock:easymockclassextension:${easymockVersion}"
334343
testCompile("javax.persistence:persistence-api:1.0")
335344
testCompile("org.aspectj:aspectjweaver:${aspectjVersion}")
336345
}
@@ -678,7 +687,6 @@ project("spring-test-mvc") {
678687
testCompile("javax.activation:activation:1.1")
679688
testCompile("javax.mail:mail:1.4")
680689
testCompile("javax.xml.bind:jaxb-api:2.2.6")
681-
testCompile("org.easymock:easymockclassextension:${easymockVersion}")
682690
testCompile("org.apache.tiles:tiles-request-api:1.0.1")
683691
testCompile("org.apache.tiles:tiles-api:3.0.1")
684692
testCompile("org.apache.tiles:tiles-core:3.0.1") {

spring-aop/src/test/java/org/springframework/aop/framework/adapter/ThrowsAdviceInterceptorTests.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import javax.transaction.TransactionRolledbackException;
2424

2525
import org.aopalliance.intercept.MethodInvocation;
26-
import static org.easymock.EasyMock.*;
2726
import static org.junit.Assert.*;
27+
import static org.mockito.BDDMockito.given;
28+
import static org.mockito.Mockito.mock;
29+
2830
import org.junit.Test;
2931
import test.aop.MethodCounter;
3032

@@ -47,12 +49,10 @@ public void testNotInvoked() throws Throwable {
4749
MyThrowsHandler th = new MyThrowsHandler();
4850
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
4951
Object ret = new Object();
50-
MethodInvocation mi = createMock(MethodInvocation.class);
51-
expect(mi.proceed()).andReturn(ret);
52-
replay(mi);
52+
MethodInvocation mi = mock(MethodInvocation.class);
53+
given(mi.proceed()).willReturn(ret);
5354
assertEquals(ret, ti.invoke(mi));
5455
assertEquals(0, th.getCalls());
55-
verify(mi);
5656
}
5757

5858
@Test
@@ -61,9 +61,8 @@ public void testNoHandlerMethodForThrowable() throws Throwable {
6161
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
6262
assertEquals(2, ti.getHandlerMethodCount());
6363
Exception ex = new Exception();
64-
MethodInvocation mi = createMock(MethodInvocation.class);
65-
expect(mi.proceed()).andThrow(ex);
66-
replay(mi);
64+
MethodInvocation mi = mock(MethodInvocation.class);
65+
given(mi.proceed()).willThrow(ex);
6766
try {
6867
ti.invoke(mi);
6968
fail();
@@ -72,20 +71,17 @@ public void testNoHandlerMethodForThrowable() throws Throwable {
7271
assertEquals(ex, caught);
7372
}
7473
assertEquals(0, th.getCalls());
75-
verify(mi);
7674
}
7775

7876
@Test
7977
public void testCorrectHandlerUsed() throws Throwable {
8078
MyThrowsHandler th = new MyThrowsHandler();
8179
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
8280
FileNotFoundException ex = new FileNotFoundException();
83-
MethodInvocation mi = createMock(MethodInvocation.class);
84-
expect(mi.getMethod()).andReturn(Object.class.getMethod("hashCode", (Class[]) null));
85-
expect(mi.getArguments()).andReturn(null);
86-
expect(mi.getThis()).andReturn(new Object());
87-
expect(mi.proceed()).andThrow(ex);
88-
replay(mi);
81+
MethodInvocation mi = mock(MethodInvocation.class);
82+
given(mi.getMethod()).willReturn(Object.class.getMethod("hashCode", (Class[]) null));
83+
given(mi.getThis()).willReturn(new Object());
84+
given(mi.proceed()).willThrow(ex);
8985
try {
9086
ti.invoke(mi);
9187
fail();
@@ -95,7 +91,6 @@ public void testCorrectHandlerUsed() throws Throwable {
9591
}
9692
assertEquals(1, th.getCalls());
9793
assertEquals(1, th.getCalls("ioException"));
98-
verify(mi);
9994
}
10095

10196
@Test
@@ -104,9 +99,8 @@ public void testCorrectHandlerUsedForSubclass() throws Throwable {
10499
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
105100
// Extends RemoteException
106101
TransactionRolledbackException ex = new TransactionRolledbackException();
107-
MethodInvocation mi = createMock(MethodInvocation.class);
108-
expect(mi.proceed()).andThrow(ex);
109-
replay(mi);
102+
MethodInvocation mi = mock(MethodInvocation.class);
103+
given(mi.proceed()).willThrow(ex);
110104
try {
111105
ti.invoke(mi);
112106
fail();
@@ -116,7 +110,6 @@ public void testCorrectHandlerUsedForSubclass() throws Throwable {
116110
}
117111
assertEquals(1, th.getCalls());
118112
assertEquals(1, th.getCalls("remoteException"));
119-
verify(mi);
120113
}
121114

122115
@Test
@@ -135,9 +128,8 @@ public void afterThrowing(RemoteException ex) throws Throwable {
135128
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
136129
// Extends RemoteException
137130
TransactionRolledbackException ex = new TransactionRolledbackException();
138-
MethodInvocation mi = createMock(MethodInvocation.class);
139-
expect(mi.proceed()).andThrow(ex);
140-
replay(mi);
131+
MethodInvocation mi = mock(MethodInvocation.class);
132+
given(mi.proceed()).willThrow(ex);
141133
try {
142134
ti.invoke(mi);
143135
fail();
@@ -147,7 +139,6 @@ public void afterThrowing(RemoteException ex) throws Throwable {
147139
}
148140
assertEquals(1, th.getCalls());
149141
assertEquals(1, th.getCalls("remoteException"));
150-
verify(mi);
151142
}
152143

153144
@SuppressWarnings("serial")

spring-aop/src/test/java/org/springframework/aop/interceptor/CustomizableTraceInterceptorTests.java

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616

1717
package org.springframework.aop.interceptor;
1818

19-
import static org.easymock.EasyMock.*;
2019
import static org.junit.Assert.*;
20+
import static org.mockito.BDDMockito.given;
21+
import static org.mockito.Matchers.anyString;
22+
import static org.mockito.Matchers.eq;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.times;
25+
import static org.mockito.Mockito.verify;
2126

2227
import java.lang.reflect.Method;
2328

@@ -83,47 +88,32 @@ public void testSetExceptionMethodWithReturnValuePlaceholder() {
8388

8489
@Test
8590
public void testSunnyDayPathLogsCorrectly() throws Throwable {
86-
Log log = createMock(Log.class);
8791

88-
MethodInvocation methodInvocation = createMock(MethodInvocation.class);
92+
MethodInvocation methodInvocation = mock(MethodInvocation.class);
93+
given(methodInvocation.getMethod()).willReturn(String.class.getMethod("toString", new Class[]{}));
94+
given(methodInvocation.getThis()).willReturn(this);
8995

90-
Method toString = String.class.getMethod("toString", new Class[]{});
91-
92-
expect(log.isTraceEnabled()).andReturn(true);
93-
expect(methodInvocation.getMethod()).andReturn(toString).times(4);
94-
expect(methodInvocation.getThis()).andReturn(this).times(2);
95-
log.trace(isA(String.class));
96-
expect(methodInvocation.proceed()).andReturn(null);
97-
log.trace(isA(String.class));
98-
99-
replay(methodInvocation);
100-
replay(log);
96+
Log log = mock(Log.class);
97+
given(log.isTraceEnabled()).willReturn(true);
10198

10299
CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
103100
interceptor.invoke(methodInvocation);
104101

105-
verify(log);
106-
verify(methodInvocation);
102+
verify(log, times(2)).trace(anyString());
107103
}
108104

109105
@Test
110106
public void testExceptionPathLogsCorrectly() throws Throwable {
111-
Log log = createMock(Log.class);
112107

113-
MethodInvocation methodInvocation = createMock(MethodInvocation.class);
108+
MethodInvocation methodInvocation = mock(MethodInvocation.class);
114109

115-
Method toString = String.class.getMethod("toString", new Class[]{});
116-
117-
expect(log.isTraceEnabled()).andReturn(true);
118-
expect(methodInvocation.getMethod()).andReturn(toString).times(4);
119-
expect(methodInvocation.getThis()).andReturn(this).times(2);
120-
log.trace(isA(String.class));
121110
IllegalArgumentException exception = new IllegalArgumentException();
122-
expect(methodInvocation.proceed()).andThrow(exception);
123-
log.trace(isA(String.class), eq(exception));
111+
given(methodInvocation.getMethod()).willReturn(String.class.getMethod("toString", new Class[]{}));
112+
given(methodInvocation.getThis()).willReturn(this);
113+
given(methodInvocation.proceed()).willThrow(exception);
124114

125-
replay(log);
126-
replay(methodInvocation);
115+
Log log = mock(Log.class);
116+
given(log.isTraceEnabled()).willReturn(true);
127117

128118
CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
129119
try {
@@ -133,29 +123,22 @@ public void testExceptionPathLogsCorrectly() throws Throwable {
133123
catch (IllegalArgumentException expected) {
134124
}
135125

136-
verify(log);
137-
verify(methodInvocation);
126+
verify(log).trace(anyString());
127+
verify(log).trace(anyString(), eq(exception));
138128
}
139129

140130
@Test
141131
public void testSunnyDayPathLogsCorrectlyWithPrettyMuchAllPlaceholdersMatching() throws Throwable {
142-
Log log = createMock(Log.class);
143-
144-
MethodInvocation methodInvocation = createMock(MethodInvocation.class);
145132

146-
Method toString = String.class.getMethod("toString", new Class[0]);
147-
Object[] arguments = new Object[]{"$ One \\$", new Long(2)};
133+
MethodInvocation methodInvocation = mock(MethodInvocation.class);
148134

149-
expect(log.isTraceEnabled()).andReturn(true);
150-
expect(methodInvocation.getMethod()).andReturn(toString).times(7);
151-
expect(methodInvocation.getThis()).andReturn(this).times(2);
152-
expect(methodInvocation.getArguments()).andReturn(arguments).times(2);
153-
log.trace(isA(String.class));
154-
expect(methodInvocation.proceed()).andReturn("Hello!");
155-
log.trace(isA(String.class));
135+
given(methodInvocation.getMethod()).willReturn(String.class.getMethod("toString", new Class[0]));
136+
given(methodInvocation.getThis()).willReturn(this);
137+
given(methodInvocation.getArguments()).willReturn(new Object[]{"$ One \\$", new Long(2)});
138+
given(methodInvocation.proceed()).willReturn("Hello!");
156139

157-
replay(methodInvocation);
158-
replay(log);
140+
Log log = mock(Log.class);
141+
given(log.isTraceEnabled()).willReturn(true);
159142

160143
CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
161144
interceptor.setEnterMessage(new StringBuffer()
@@ -174,8 +157,7 @@ public void testSunnyDayPathLogsCorrectlyWithPrettyMuchAllPlaceholdersMatching()
174157
.append("' this long.").toString());
175158
interceptor.invoke(methodInvocation);
176159

177-
verify(log);
178-
verify(methodInvocation);
160+
verify(log, times(2)).trace(anyString());
179161
}
180162

181163

spring-aop/src/test/java/org/springframework/aop/interceptor/DebugInterceptorTests.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616

1717
package org.springframework.aop.interceptor;
1818

19-
import static org.easymock.EasyMock.*;
2019
import static org.junit.Assert.*;
20+
import static org.mockito.BDDMockito.given;
21+
import static org.mockito.Matchers.anyString;
22+
import static org.mockito.Matchers.eq;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.times;
25+
import static org.mockito.Mockito.verify;
2126

2227
import org.aopalliance.intercept.MethodInvocation;
2328
import org.apache.commons.logging.Log;
@@ -33,40 +38,29 @@ public final class DebugInterceptorTests {
3338

3439
@Test
3540
public void testSunnyDayPathLogsCorrectly() throws Throwable {
36-
Log log = createMock(Log.class);
3741

38-
MethodInvocation methodInvocation = createMock(MethodInvocation.class);
42+
MethodInvocation methodInvocation = mock(MethodInvocation.class);
3943

40-
expect(log.isTraceEnabled()).andReturn(true);
41-
log.trace(isA(String.class));
42-
expect(methodInvocation.proceed()).andReturn(null);
43-
log.trace(isA(String.class));
44-
45-
replay(methodInvocation);
46-
replay(log);
44+
Log log = mock(Log.class);
45+
given(log.isTraceEnabled()).willReturn(true);
4746

4847
DebugInterceptor interceptor = new StubDebugInterceptor(log);
4948
interceptor.invoke(methodInvocation);
5049
checkCallCountTotal(interceptor);
5150

52-
verify(methodInvocation);
53-
verify(log);
51+
verify(log, times(2)).trace(anyString());
5452
}
5553

5654
@Test
5755
public void testExceptionPathStillLogsCorrectly() throws Throwable {
58-
Log log = createMock(Log.class);
5956

60-
MethodInvocation methodInvocation = createMock(MethodInvocation.class);
57+
MethodInvocation methodInvocation = mock(MethodInvocation.class);
6158

62-
expect(log.isTraceEnabled()).andReturn(true);
63-
log.trace(isA(String.class));
6459
IllegalArgumentException exception = new IllegalArgumentException();
65-
expect(methodInvocation.proceed()).andThrow(exception);
66-
log.trace(isA(String.class), eq(exception));
60+
given(methodInvocation.proceed()).willThrow(exception);
6761

68-
replay(methodInvocation);
69-
replay(log);
62+
Log log = mock(Log.class);
63+
given(log.isTraceEnabled()).willReturn(true);
7064

7165
DebugInterceptor interceptor = new StubDebugInterceptor(log);
7266
try {
@@ -76,8 +70,8 @@ public void testExceptionPathStillLogsCorrectly() throws Throwable {
7670
}
7771
checkCallCountTotal(interceptor);
7872

79-
verify(methodInvocation);
80-
verify(log);
73+
verify(log).trace(anyString());
74+
verify(log).trace(anyString(), eq(exception));
8175
}
8276

8377
private void checkCallCountTotal(DebugInterceptor interceptor) {

0 commit comments

Comments
 (0)