Skip to content

Commit 9d74da0

Browse files
committed
Migrate JUnit 4 assertions to AssertJ
Migrate all existing JUnit 4 `assert...` based assertions to AssertJ and add a checkstyle rule to ensure they don't return. See gh-23022
1 parent 95a9d46 commit 9d74da0

File tree

1,636 files changed

+37947
-40476
lines changed

Some content is hidden

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

1,636 files changed

+37947
-40476
lines changed

spring-aop/src/test/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscovererTests.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323

2424
import org.springframework.aop.aspectj.AspectJAdviceParameterNameDiscoverer.AmbiguousBindingException;
2525

26+
import static org.assertj.core.api.Assertions.assertThat;
2627
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
27-
import static org.junit.Assert.assertEquals;
28-
import static org.junit.Assert.assertNotNull;
2928

3029
/**
3130
* Unit tests for the {@link AspectJAdviceParameterNameDiscoverer} class.
@@ -236,8 +235,7 @@ protected void assertParameterNames(Method method, String pointcut, String[] par
236235
protected void assertParameterNames(
237236
Method method, String pointcut, String returning, String throwing, String[] parameterNames) {
238237

239-
assertEquals("bad test specification, must have same number of parameter names as method arguments",
240-
method.getParameterCount(), parameterNames.length);
238+
assertThat(parameterNames.length).as("bad test specification, must have same number of parameter names as method arguments").isEqualTo(method.getParameterCount());
241239

242240
AspectJAdviceParameterNameDiscoverer discoverer = new AspectJAdviceParameterNameDiscoverer(pointcut);
243241
discoverer.setRaiseExceptions(true);
@@ -248,16 +246,14 @@ protected void assertParameterNames(
248246
String formattedExpectedNames = format(parameterNames);
249247
String formattedActualNames = format(discoveredNames);
250248

251-
assertEquals("Expecting " + parameterNames.length + " parameter names in return set '" +
249+
assertThat(discoveredNames.length).as("Expecting " + parameterNames.length + " parameter names in return set '" +
252250
formattedExpectedNames + "', but found " + discoveredNames.length +
253-
" '" + formattedActualNames + "'",
254-
parameterNames.length, discoveredNames.length);
251+
" '" + formattedActualNames + "'").isEqualTo(parameterNames.length);
255252

256253
for (int i = 0; i < discoveredNames.length; i++) {
257-
assertNotNull("Parameter names must never be null", discoveredNames[i]);
258-
assertEquals("Expecting parameter " + i + " to be named '" +
259-
parameterNames[i] + "' but was '" + discoveredNames[i] + "'",
260-
parameterNames[i], discoveredNames[i]);
254+
assertThat(discoveredNames[i]).as("Parameter names must never be null").isNotNull();
255+
assertThat(discoveredNames[i]).as("Expecting parameter " + i + " to be named '" +
256+
parameterNames[i] + "' but was '" + discoveredNames[i] + "'").isEqualTo(parameterNames[i]);
261257
}
262258
}
263259

spring-aop/src/test/java/org/springframework/aop/aspectj/AspectJExpressionPointcutTests.java

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@
4040
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4141
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
4242
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
43-
import static org.junit.Assert.assertEquals;
44-
import static org.junit.Assert.assertFalse;
45-
import static org.junit.Assert.assertTrue;
4643

4744
/**
4845
* @author Rob Harrop
@@ -81,9 +78,9 @@ public void testMatchExplicit() {
8178
// not currently testable in a reliable fashion
8279
//assertDoesNotMatchStringClass(classFilter);
8380

84-
assertFalse("Should not be a runtime match", methodMatcher.isRuntime());
81+
assertThat(methodMatcher.isRuntime()).as("Should not be a runtime match").isFalse();
8582
assertMatchesGetAge(methodMatcher);
86-
assertFalse("Expression should match setAge() method", methodMatcher.matches(setAge, TestBean.class));
83+
assertThat(methodMatcher.matches(setAge, TestBean.class)).as("Expression should match setAge() method").isFalse();
8784
}
8885

8986
@Test
@@ -99,9 +96,9 @@ public void testMatchWithTypePattern() throws Exception {
9996
// not currently testable in a reliable fashion
10097
//assertDoesNotMatchStringClass(classFilter);
10198

102-
assertFalse("Should not be a runtime match", methodMatcher.isRuntime());
99+
assertThat(methodMatcher.isRuntime()).as("Should not be a runtime match").isFalse();
103100
assertMatchesGetAge(methodMatcher);
104-
assertTrue("Expression should match setAge(int) method", methodMatcher.matches(setAge, TestBean.class));
101+
assertThat(methodMatcher.matches(setAge, TestBean.class)).as("Expression should match setAge(int) method").isTrue();
105102
}
106103

107104

@@ -128,10 +125,10 @@ private void testThisOrTarget(String which) throws SecurityException, NoSuchMeth
128125
AspectJExpressionPointcut iOtherPc = new AspectJExpressionPointcut();
129126
iOtherPc.setExpression(matchesIOther);
130127

131-
assertTrue(testBeanPc.matches(TestBean.class));
132-
assertTrue(testBeanPc.matches(getAge, TestBean.class));
133-
assertTrue(iOtherPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class));
134-
assertFalse(testBeanPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class));
128+
assertThat(testBeanPc.matches(TestBean.class)).isTrue();
129+
assertThat(testBeanPc.matches(getAge, TestBean.class)).isTrue();
130+
assertThat(iOtherPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class)).isTrue();
131+
assertThat(testBeanPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class)).isFalse();
135132
}
136133

137134
@Test
@@ -154,13 +151,13 @@ private void testWithinPackage(boolean matchSubpackages) throws SecurityExceptio
154151
AspectJExpressionPointcut withinBeansPc = new AspectJExpressionPointcut();
155152
withinBeansPc.setExpression(withinBeansPackage);
156153

157-
assertTrue(withinBeansPc.matches(TestBean.class));
158-
assertTrue(withinBeansPc.matches(getAge, TestBean.class));
159-
assertEquals(matchSubpackages, withinBeansPc.matches(DeepBean.class));
160-
assertEquals(matchSubpackages, withinBeansPc.matches(
161-
DeepBean.class.getMethod("aMethod", String.class), DeepBean.class));
162-
assertFalse(withinBeansPc.matches(String.class));
163-
assertFalse(withinBeansPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class));
154+
assertThat(withinBeansPc.matches(TestBean.class)).isTrue();
155+
assertThat(withinBeansPc.matches(getAge, TestBean.class)).isTrue();
156+
assertThat(withinBeansPc.matches(DeepBean.class)).isEqualTo(matchSubpackages);
157+
assertThat(withinBeansPc.matches(
158+
DeepBean.class.getMethod("aMethod", String.class), DeepBean.class)).isEqualTo(matchSubpackages);
159+
assertThat(withinBeansPc.matches(String.class)).isFalse();
160+
assertThat(withinBeansPc.matches(OtherIOther.class.getMethod("absquatulate"), OtherIOther.class)).isFalse();
164161
}
165162

166163
@Test
@@ -201,12 +198,10 @@ public void testMatchWithArgs() throws Exception {
201198
// not currently testable in a reliable fashion
202199
//assertDoesNotMatchStringClass(classFilter);
203200

204-
assertTrue("Should match with setSomeNumber with Double input",
205-
methodMatcher.matches(setSomeNumber, TestBean.class, new Double(12)));
206-
assertFalse("Should not match setSomeNumber with Integer input",
207-
methodMatcher.matches(setSomeNumber, TestBean.class, new Integer(11)));
208-
assertFalse("Should not match getAge", methodMatcher.matches(getAge, TestBean.class));
209-
assertTrue("Should be a runtime match", methodMatcher.isRuntime());
201+
assertThat(methodMatcher.matches(setSomeNumber, TestBean.class, new Double(12))).as("Should match with setSomeNumber with Double input").isTrue();
202+
assertThat(methodMatcher.matches(setSomeNumber, TestBean.class, new Integer(11))).as("Should not match setSomeNumber with Integer input").isFalse();
203+
assertThat(methodMatcher.matches(getAge, TestBean.class)).as("Should not match getAge").isFalse();
204+
assertThat(methodMatcher.isRuntime()).as("Should be a runtime match").isTrue();
210205
}
211206

212207
@Test
@@ -215,11 +210,11 @@ public void testSimpleAdvice() {
215210
CallCountingInterceptor interceptor = new CallCountingInterceptor();
216211
TestBean testBean = getAdvisedProxy(expression, interceptor);
217212

218-
assertEquals("Calls should be 0", 0, interceptor.getCount());
213+
assertThat(interceptor.getCount()).as("Calls should be 0").isEqualTo(0);
219214
testBean.getAge();
220-
assertEquals("Calls should be 1", 1, interceptor.getCount());
215+
assertThat(interceptor.getCount()).as("Calls should be 1").isEqualTo(1);
221216
testBean.setAge(90);
222-
assertEquals("Calls should still be 1", 1, interceptor.getCount());
217+
assertThat(interceptor.getCount()).as("Calls should still be 1").isEqualTo(1);
223218
}
224219

225220
@Test
@@ -228,12 +223,12 @@ public void testDynamicMatchingProxy() {
228223
CallCountingInterceptor interceptor = new CallCountingInterceptor();
229224
TestBean testBean = getAdvisedProxy(expression, interceptor);
230225

231-
assertEquals("Calls should be 0", 0, interceptor.getCount());
226+
assertThat(interceptor.getCount()).as("Calls should be 0").isEqualTo(0);
232227
testBean.setSomeNumber(new Double(30));
233-
assertEquals("Calls should be 1", 1, interceptor.getCount());
228+
assertThat(interceptor.getCount()).as("Calls should be 1").isEqualTo(1);
234229

235230
testBean.setSomeNumber(new Integer(90));
236-
assertEquals("Calls should be 1", 1, interceptor.getCount());
231+
assertThat(interceptor.getCount()).as("Calls should be 1").isEqualTo(1);
237232
}
238233

239234
@Test
@@ -260,11 +255,11 @@ private TestBean getAdvisedProxy(String pointcutExpression, CallCountingIntercep
260255
}
261256

262257
private void assertMatchesGetAge(MethodMatcher methodMatcher) {
263-
assertTrue("Expression should match getAge() method", methodMatcher.matches(getAge, TestBean.class));
258+
assertThat(methodMatcher.matches(getAge, TestBean.class)).as("Expression should match getAge() method").isTrue();
264259
}
265260

266261
private void assertMatchesTestBeanClass(ClassFilter classFilter) {
267-
assertTrue("Expression should match TestBean class", classFilter.matches(TestBean.class));
262+
assertThat(classFilter.matches(TestBean.class)).as("Expression should match TestBean class").isTrue();
268263
}
269264

270265
@Test
@@ -279,14 +274,14 @@ public void testWithUnsupportedPointcutPrimitive() {
279274
public void testAndSubstitution() {
280275
Pointcut pc = getPointcut("execution(* *(..)) and args(String)");
281276
PointcutExpression expr = ((AspectJExpressionPointcut) pc).getPointcutExpression();
282-
assertEquals("execution(* *(..)) && args(String)",expr.getPointcutExpression());
277+
assertThat(expr.getPointcutExpression()).isEqualTo("execution(* *(..)) && args(String)");
283278
}
284279

285280
@Test
286281
public void testMultipleAndSubstitutions() {
287282
Pointcut pc = getPointcut("execution(* *(..)) and args(String) and this(Object)");
288283
PointcutExpression expr = ((AspectJExpressionPointcut) pc).getPointcutExpression();
289-
assertEquals("execution(* *(..)) && args(String) && this(Object)",expr.getPointcutExpression());
284+
assertThat(expr.getPointcutExpression()).isEqualTo("execution(* *(..)) && args(String) && this(Object)");
290285
}
291286

292287
private Pointcut getPointcut(String expression) {

spring-aop/src/test/java/org/springframework/aop/aspectj/BeanNamePointcutMatchingTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
import org.springframework.tests.sample.beans.TestBean;
2222

23-
import static org.junit.Assert.assertFalse;
24-
import static org.junit.Assert.assertTrue;
23+
import static org.assertj.core.api.Assertions.assertThat;
2524

2625
/**
2726
* Tests for matching of bean() pointcut designator.
@@ -80,13 +79,11 @@ public void testNonMatchingPointcuts() {
8079

8180

8281
private void assertMatch(String beanName, String pcExpression) {
83-
assertTrue("Unexpected mismatch for bean \"" + beanName + "\" for pcExpression \"" + pcExpression + "\"",
84-
matches(beanName, pcExpression));
82+
assertThat(matches(beanName, pcExpression)).as("Unexpected mismatch for bean \"" + beanName + "\" for pcExpression \"" + pcExpression + "\"").isTrue();
8583
}
8684

8785
private void assertMisMatch(String beanName, String pcExpression) {
88-
assertFalse("Unexpected match for bean \"" + beanName + "\" for pcExpression \"" + pcExpression + "\"",
89-
matches(beanName, pcExpression));
86+
assertThat(matches(beanName, pcExpression)).as("Unexpected match for bean \"" + beanName + "\" for pcExpression \"" + pcExpression + "\"").isFalse();
9087
}
9188

9289
private static boolean matches(final String beanName, String pcExpression) {

spring-aop/src/test/java/org/springframework/aop/aspectj/MethodInvocationProceedingJoinPointTests.java

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,9 @@
3737
import org.springframework.tests.sample.beans.ITestBean;
3838
import org.springframework.tests.sample.beans.TestBean;
3939

40+
import static org.assertj.core.api.Assertions.assertThat;
4041
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4142
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
42-
import static org.junit.Assert.assertEquals;
43-
import static org.junit.Assert.assertFalse;
44-
import static org.junit.Assert.assertNotSame;
45-
import static org.junit.Assert.assertSame;
46-
import static org.junit.Assert.assertTrue;
4743

4844
/**
4945
* @author Rod Johnson
@@ -80,21 +76,21 @@ public void testCanGetMethodSignatureFromJoinPoint() {
8076
@Override
8177
public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
8278
JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
83-
assertTrue("Method named in toString", jp.toString().contains(method.getName()));
79+
assertThat(jp.toString().contains(method.getName())).as("Method named in toString").isTrue();
8480
// Ensure that these don't cause problems
8581
jp.toShortString();
8682
jp.toLongString();
8783

88-
assertSame(target, AbstractAspectJAdvice.currentJoinPoint().getTarget());
89-
assertFalse(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget()));
84+
assertThat(AbstractAspectJAdvice.currentJoinPoint().getTarget()).isSameAs(target);
85+
assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget())).isFalse();
9086

9187
ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
92-
assertTrue(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis()));
88+
assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis())).isTrue();
9389

94-
assertNotSame(target, thisProxy);
90+
assertThat(thisProxy).isNotSameAs(target);
9591

9692
// Check getting again doesn't cause a problem
97-
assertSame(thisProxy, AbstractAspectJAdvice.currentJoinPoint().getThis());
93+
assertThat(AbstractAspectJAdvice.currentJoinPoint().getThis()).isSameAs(thisProxy);
9894

9995
// Try reentrant call--will go through this advice.
10096
// Be sure to increment depth to avoid infinite recursion
@@ -103,29 +99,29 @@ public void before(Method method, Object[] args, @Nullable Object target) throws
10399
thisProxy.toString();
104100
// Change age, so this will be returned by invocation
105101
thisProxy.setAge(newAge);
106-
assertEquals(newAge, thisProxy.getAge());
102+
assertThat(thisProxy.getAge()).isEqualTo(newAge);
107103
}
108104

109-
assertSame(AopContext.currentProxy(), thisProxy);
110-
assertSame(target, raw);
105+
assertThat(thisProxy).isSameAs(AopContext.currentProxy());
106+
assertThat(raw).isSameAs(target);
111107

112-
assertSame(method.getName(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getName());
113-
assertEquals(method.getModifiers(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers());
108+
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getName()).isSameAs(method.getName());
109+
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers()).isEqualTo(method.getModifiers());
114110

115111
MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
116-
assertSame("Return same MethodSignature repeatedly", msig, AbstractAspectJAdvice.currentJoinPoint().getSignature());
117-
assertSame("Return same JoinPoint repeatedly", AbstractAspectJAdvice.currentJoinPoint(), AbstractAspectJAdvice.currentJoinPoint());
118-
assertEquals(method.getDeclaringClass(), msig.getDeclaringType());
119-
assertTrue(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes()));
120-
assertEquals(method.getReturnType(), msig.getReturnType());
121-
assertTrue(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes()));
112+
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature()).as("Return same MethodSignature repeatedly").isSameAs(msig);
113+
assertThat(AbstractAspectJAdvice.currentJoinPoint()).as("Return same JoinPoint repeatedly").isSameAs(AbstractAspectJAdvice.currentJoinPoint());
114+
assertThat(msig.getDeclaringType()).isEqualTo(method.getDeclaringClass());
115+
assertThat(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes())).isTrue();
116+
assertThat(msig.getReturnType()).isEqualTo(method.getReturnType());
117+
assertThat(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes())).isTrue();
122118
msig.toLongString();
123119
msig.toShortString();
124120
}
125121
});
126122
ITestBean itb = (ITestBean) pf.getProxy();
127123
// Any call will do
128-
assertEquals("Advice reentrantly set age", newAge, itb.getAge());
124+
assertThat(itb.getAge()).as("Advice reentrantly set age").isEqualTo(newAge);
129125
}
130126

131127
@Test
@@ -137,8 +133,8 @@ public void testCanGetSourceLocationFromJoinPoint() {
137133
@Override
138134
public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
139135
SourceLocation sloc = AbstractAspectJAdvice.currentJoinPoint().getSourceLocation();
140-
assertEquals("Same source location must be returned on subsequent requests", sloc, AbstractAspectJAdvice.currentJoinPoint().getSourceLocation());
141-
assertEquals(TestBean.class, sloc.getWithinType());
136+
assertThat(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation()).as("Same source location must be returned on subsequent requests").isEqualTo(sloc);
137+
assertThat(sloc.getWithinType()).isEqualTo(TestBean.class);
142138
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getLine);
143139
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getFileName);
144140
}
@@ -157,10 +153,10 @@ public void testCanGetStaticPartFromJoinPoint() {
157153
@Override
158154
public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
159155
StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart();
160-
assertEquals("Same static part must be returned on subsequent requests", staticPart, AbstractAspectJAdvice.currentJoinPoint().getStaticPart());
161-
assertEquals(ProceedingJoinPoint.METHOD_EXECUTION, staticPart.getKind());
162-
assertSame(AbstractAspectJAdvice.currentJoinPoint().getSignature(), staticPart.getSignature());
163-
assertEquals(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation(), staticPart.getSourceLocation());
156+
assertThat(AbstractAspectJAdvice.currentJoinPoint().getStaticPart()).as("Same static part must be returned on subsequent requests").isEqualTo(staticPart);
157+
assertThat(staticPart.getKind()).isEqualTo(ProceedingJoinPoint.METHOD_EXECUTION);
158+
assertThat(staticPart.getSignature()).isSameAs(AbstractAspectJAdvice.currentJoinPoint().getSignature());
159+
assertThat(staticPart.getSourceLocation()).isEqualTo(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation());
164160
}
165161
});
166162
ITestBean itb = (ITestBean) pf.getProxy();
@@ -181,13 +177,13 @@ public void before(Method method, Object[] args, @Nullable Object target) throws
181177
JoinPoint.StaticPart aspectJVersionJp = Factory.makeEncSJP(method);
182178
JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
183179

184-
assertEquals(aspectJVersionJp.getSignature().toLongString(), jp.getSignature().toLongString());
185-
assertEquals(aspectJVersionJp.getSignature().toShortString(), jp.getSignature().toShortString());
186-
assertEquals(aspectJVersionJp.getSignature().toString(), jp.getSignature().toString());
180+
assertThat(jp.getSignature().toLongString()).isEqualTo(aspectJVersionJp.getSignature().toLongString());
181+
assertThat(jp.getSignature().toShortString()).isEqualTo(aspectJVersionJp.getSignature().toShortString());
182+
assertThat(jp.getSignature().toString()).isEqualTo(aspectJVersionJp.getSignature().toString());
187183

188-
assertEquals(aspectJVersionJp.toLongString(), jp.toLongString());
189-
assertEquals(aspectJVersionJp.toShortString(), jp.toShortString());
190-
assertEquals(aspectJVersionJp.toString(), jp.toString());
184+
assertThat(jp.toLongString()).isEqualTo(aspectJVersionJp.toLongString());
185+
assertThat(jp.toShortString()).isEqualTo(aspectJVersionJp.toShortString());
186+
assertThat(jp.toString()).isEqualTo(aspectJVersionJp.toString());
191187
}
192188
});
193189
ITestBean itb = (ITestBean) pf.getProxy();

0 commit comments

Comments
 (0)