Skip to content

Commit 1734dec

Browse files
authored
Refactor AssertJ assertions into more idiomatic ones
This commit refactors some AssertJ assertions into more idiomatic and readable ones. Using the dedicated assertion instead of a generic one will produce more meaningful error messages. For instance, consider collection size: ``` // expected: 5 but was: 2 assertThat(collection.size()).equals(5); // Expected size: 5 but was: 2 in: [1, 2] assertThat(collection).hasSize(5); ``` Closes gh-30104
1 parent dd97ee4 commit 1734dec

File tree

371 files changed

+3179
-3078
lines changed

Some content is hidden

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

371 files changed

+3179
-3078
lines changed

integration-tests/src/test/java/org/springframework/aop/config/AopNamespaceHandlerScopeIntegrationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -99,7 +99,7 @@ void testRequestScoping() throws Exception {
9999
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(oldRequest));
100100
assertThat(requestScoped.getName()).isEqualTo(bram);
101101

102-
assertThat(((Advised) requestScoped).getAdvisors().length > 0).as("Should have advisors").isTrue();
102+
assertThat(((Advised) requestScoped).getAdvisors()).as("Should have advisors").isNotEmpty();
103103
}
104104

105105
@Test
@@ -131,7 +131,7 @@ void testSessionScoping() throws Exception {
131131
request.setSession(oldSession);
132132
assertThat(sessionScoped.getName()).isEqualTo(bram);
133133

134-
assertThat(((Advised) sessionScoped).getAdvisors().length > 0).as("Should have advisors").isTrue();
134+
assertThat(((Advised) sessionScoped).getAdvisors()).as("Should have advisors").isNotEmpty();
135135
}
136136

137137
}

integration-tests/src/test/java/org/springframework/beans/factory/xml/ComponentBeanDefinitionParserTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,25 +52,25 @@ void tearDown() {
5252
@Test
5353
void testBionicBasic() {
5454
Component cp = getBionicFamily();
55-
assertThat("Bionic-1").isEqualTo(cp.getName());
55+
assertThat(cp.getName()).isEqualTo("Bionic-1");
5656
}
5757

5858
@Test
5959
void testBionicFirstLevelChildren() {
6060
Component cp = getBionicFamily();
6161
List<Component> components = cp.getComponents();
62-
assertThat(2).isEqualTo(components.size());
63-
assertThat("Mother-1").isEqualTo(components.get(0).getName());
64-
assertThat("Rock-1").isEqualTo(components.get(1).getName());
62+
assertThat(components).hasSize(2);
63+
assertThat(components.get(0).getName()).isEqualTo("Mother-1");
64+
assertThat(components.get(1).getName()).isEqualTo("Rock-1");
6565
}
6666

6767
@Test
6868
void testBionicSecondLevelChildren() {
6969
Component cp = getBionicFamily();
7070
List<Component> components = cp.getComponents().get(0).getComponents();
71-
assertThat(2).isEqualTo(components.size());
72-
assertThat("Karate-1").isEqualTo(components.get(0).getName());
73-
assertThat("Sport-1").isEqualTo(components.get(1).getName());
71+
assertThat(components).hasSize(2);
72+
assertThat(components.get(0).getName()).isEqualTo("Karate-1");
73+
assertThat(components.get(1).getName()).isEqualTo("Sport-1");
7474
}
7575

7676
private Component getBionicFamily() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,7 +70,7 @@ public void testCanGetMethodSignatureFromJoinPoint() {
7070
AtomicInteger depth = new AtomicInteger();
7171
pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
7272
JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
73-
assertThat(jp.toString().contains(method.getName())).as("Method named in toString").isTrue();
73+
assertThat(jp.toString()).as("Method named in toString").contains(method.getName());
7474
// Ensure that these don't cause problems
7575
jp.toShortString();
7676
jp.toLongString();

spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactoryTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,10 @@ void bindingWithMultipleArgsDifferentlyOrdered() {
319319
@Test
320320
void introductionOnTargetNotImplementingInterface() {
321321
NotLockable notLockableTarget = new NotLockable();
322-
assertThat(notLockableTarget instanceof Lockable).isFalse();
322+
assertThat(notLockableTarget).isNotInstanceOf(Lockable.class);
323323
NotLockable notLockable1 = createProxy(notLockableTarget, NotLockable.class,
324324
getAdvisorFactory().getAdvisors(aspectInstanceFactory(new MakeLockable(), "someBean")));
325-
assertThat(notLockable1 instanceof Lockable).isTrue();
325+
assertThat(notLockable1).isInstanceOf(Lockable.class);
326326
Lockable lockable = (Lockable) notLockable1;
327327
assertThat(lockable.locked()).isFalse();
328328
lockable.lock();
@@ -331,7 +331,7 @@ void introductionOnTargetNotImplementingInterface() {
331331
NotLockable notLockable2Target = new NotLockable();
332332
NotLockable notLockable2 = createProxy(notLockable2Target, NotLockable.class,
333333
getAdvisorFactory().getAdvisors(aspectInstanceFactory(new MakeLockable(), "someBean")));
334-
assertThat(notLockable2 instanceof Lockable).isTrue();
334+
assertThat(notLockable2).isInstanceOf(Lockable.class);
335335
Lockable lockable2 = (Lockable) notLockable2;
336336
assertThat(lockable2.locked()).isFalse();
337337
notLockable2.setIntValue(1);
@@ -345,7 +345,7 @@ void introductionAdvisorExcludedFromTargetImplementingInterface() {
345345
assertThat(AopUtils.findAdvisorsThatCanApply(
346346
getAdvisorFactory().getAdvisors(
347347
aspectInstanceFactory(new MakeLockable(), "someBean")),
348-
CannotBeUnlocked.class).isEmpty()).isTrue();
348+
CannotBeUnlocked.class)).isEmpty();
349349
assertThat(AopUtils.findAdvisorsThatCanApply(getAdvisorFactory().getAdvisors(
350350
aspectInstanceFactory(new MakeLockable(),"someBean")), NotLockable.class)).hasSize(2);
351351
}
@@ -373,7 +373,7 @@ void introductionOnTargetExcludedByTypePattern() {
373373
AopUtils.findAdvisorsThatCanApply(
374374
getAdvisorFactory().getAdvisors(aspectInstanceFactory(new MakeLockable(), "someBean")),
375375
List.class));
376-
assertThat(proxy instanceof Lockable).as("Type pattern must have excluded mixin").isFalse();
376+
assertThat(proxy).as("Type pattern must have excluded mixin").isNotInstanceOf(Lockable.class);
377377
}
378378

379379
@Test
@@ -430,7 +430,7 @@ void aspectMethodThrowsExceptionLegalOnSignature() {
430430
UnsupportedOperationException expectedException = new UnsupportedOperationException();
431431
List<Advisor> advisors = getAdvisorFactory().getAdvisors(
432432
aspectInstanceFactory(new ExceptionThrowingAspect(expectedException), "someBean"));
433-
assertThat(advisors.size()).as("One advice method was found").isEqualTo(1);
433+
assertThat(advisors).as("One advice method was found").hasSize(1);
434434
ITestBean itb = createProxy(target, ITestBean.class, advisors);
435435
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(itb::getAge);
436436
}
@@ -443,7 +443,7 @@ void aspectMethodThrowsExceptionIllegalOnSignature() {
443443
RemoteException expectedException = new RemoteException();
444444
List<Advisor> advisors = getAdvisorFactory().getAdvisors(
445445
aspectInstanceFactory(new ExceptionThrowingAspect(expectedException), "someBean"));
446-
assertThat(advisors.size()).as("One advice method was found").isEqualTo(1);
446+
assertThat(advisors).as("One advice method was found").hasSize(1);
447447
ITestBean itb = createProxy(target, ITestBean.class, advisors);
448448
assertThatExceptionOfType(UndeclaredThrowableException.class)
449449
.isThrownBy(itb::getAge)
@@ -456,7 +456,7 @@ void twoAdvicesOnOneAspect() {
456456
TwoAdviceAspect twoAdviceAspect = new TwoAdviceAspect();
457457
List<Advisor> advisors = getAdvisorFactory().getAdvisors(
458458
aspectInstanceFactory(twoAdviceAspect, "someBean"));
459-
assertThat(advisors.size()).as("Two advice methods found").isEqualTo(2);
459+
assertThat(advisors).as("Two advice methods found").hasSize(2);
460460
ITestBean itb = createProxy(target, ITestBean.class, advisors);
461461
itb.setName("");
462462
assertThat(itb.getAge()).isEqualTo(0);

spring-aop/src/test/java/org/springframework/aop/framework/ProxyFactoryTests.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@ public int compareTo(Object arg0) {
197197
TestBeanSubclass raw = new TestBeanSubclass();
198198
ProxyFactory factory = new ProxyFactory(raw);
199199
//System.out.println("Proxied interfaces are " + StringUtils.arrayToDelimitedString(factory.getProxiedInterfaces(), ","));
200-
assertThat(factory.getProxiedInterfaces().length).as("Found correct number of interfaces").isEqualTo(5);
200+
assertThat(factory.getProxiedInterfaces()).as("Found correct number of interfaces").hasSize(5);
201201
ITestBean tb = (ITestBean) factory.getProxy();
202202
assertThat(tb).as("Picked up secondary interface").isInstanceOf(IOther.class);
203203
raw.setAge(25);
204-
assertThat(tb.getAge() == raw.getAge()).isTrue();
204+
assertThat(tb.getAge()).isEqualTo(raw.getAge());
205205

206206
long t = 555555L;
207207
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t);
@@ -211,10 +211,10 @@ public int compareTo(Object arg0) {
211211
factory.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
212212

213213
Class<?>[] newProxiedInterfaces = factory.getProxiedInterfaces();
214-
assertThat(newProxiedInterfaces.length).as("Advisor proxies one more interface after introduction").isEqualTo(oldProxiedInterfaces.length + 1);
214+
assertThat(newProxiedInterfaces).as("Advisor proxies one more interface after introduction").hasSize(oldProxiedInterfaces.length + 1);
215215

216216
TimeStamped ts = (TimeStamped) factory.getProxy();
217-
assertThat(ts.getTimeStamp() == t).isTrue();
217+
assertThat(ts.getTimeStamp()).isEqualTo(t);
218218
// Shouldn't fail;
219219
((IOther) ts).absquatulate();
220220
}
@@ -234,13 +234,13 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
234234
factory.addAdvice(0, di);
235235
assertThat(factory.getProxy()).isInstanceOf(ITestBean.class);
236236
assertThat(factory.adviceIncluded(di)).isTrue();
237-
assertThat(!factory.adviceIncluded(diUnused)).isTrue();
238-
assertThat(factory.countAdvicesOfType(NopInterceptor.class) == 1).isTrue();
239-
assertThat(factory.countAdvicesOfType(MyInterceptor.class) == 0).isTrue();
237+
assertThat(factory.adviceIncluded(diUnused)).isFalse();
238+
assertThat(factory.countAdvicesOfType(NopInterceptor.class)).isEqualTo(1);
239+
assertThat(factory.countAdvicesOfType(MyInterceptor.class)).isEqualTo(0);
240240

241241
factory.addAdvice(0, diUnused);
242242
assertThat(factory.adviceIncluded(diUnused)).isTrue();
243-
assertThat(factory.countAdvicesOfType(NopInterceptor.class) == 2).isTrue();
243+
assertThat(factory.countAdvicesOfType(NopInterceptor.class)).isEqualTo(2);
244244
}
245245

246246
@Test
@@ -260,7 +260,8 @@ public void testSealedInterfaceExclusion() {
260260
public void testCanAddAndRemoveAspectInterfacesOnSingleton() {
261261
ProxyFactory config = new ProxyFactory(new TestBean());
262262

263-
assertThat(config.getProxy() instanceof TimeStamped).as("Shouldn't implement TimeStamped before manipulation").isFalse();
263+
assertThat(config.getProxy()).as("Shouldn't implement TimeStamped before manipulation")
264+
.isNotInstanceOf(TimeStamped.class);
264265

265266
long time = 666L;
266267
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
@@ -270,26 +271,26 @@ public void testCanAddAndRemoveAspectInterfacesOnSingleton() {
270271
int oldCount = config.getAdvisors().length;
271272
config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
272273

273-
assertThat(config.getAdvisors().length == oldCount + 1).isTrue();
274+
assertThat(config.getAdvisors()).hasSize(oldCount + 1);
274275

275276
TimeStamped ts = (TimeStamped) config.getProxy();
276-
assertThat(ts.getTimeStamp() == time).isTrue();
277+
assertThat(ts.getTimeStamp()).isEqualTo(time);
277278

278279
// Can remove
279280
config.removeAdvice(ti);
280281

281-
assertThat(config.getAdvisors().length == oldCount).isTrue();
282+
assertThat(config.getAdvisors()).hasSize(oldCount);
282283

283284
assertThatRuntimeException()
284285
.as("Existing object won't implement this interface any more")
285286
.isThrownBy(ts::getTimeStamp); // Existing reference will fail
286287

287-
assertThat(config.getProxy() instanceof TimeStamped).as("Should no longer implement TimeStamped").isFalse();
288+
assertThat(config.getProxy()).as("Should no longer implement TimeStamped").isNotInstanceOf(TimeStamped.class);
288289

289290
// Now check non-effect of removing interceptor that isn't there
290291
config.removeAdvice(new DebugInterceptor());
291292

292-
assertThat(config.getAdvisors().length == oldCount).isTrue();
293+
assertThat(config.getAdvisors()).hasSize(oldCount);
293294

294295
ITestBean it = (ITestBean) ts;
295296
DebugInterceptor debugInterceptor = new DebugInterceptor();
@@ -299,7 +300,7 @@ public void testCanAddAndRemoveAspectInterfacesOnSingleton() {
299300
config.removeAdvice(debugInterceptor);
300301
it.getSpouse();
301302
// not invoked again
302-
assertThat(debugInterceptor.getCount() == 1).isTrue();
303+
assertThat(debugInterceptor.getCount()).isEqualTo(1);
303304
}
304305

305306
@Test
@@ -308,13 +309,13 @@ public void testProxyTargetClassWithInterfaceAsTarget() {
308309
pf.setTargetClass(ITestBean.class);
309310
Object proxy = pf.getProxy();
310311
assertThat(AopUtils.isJdkDynamicProxy(proxy)).as("Proxy is a JDK proxy").isTrue();
311-
assertThat(proxy instanceof ITestBean).isTrue();
312+
assertThat(proxy).isInstanceOf(ITestBean.class);
312313
assertThat(AopProxyUtils.ultimateTargetClass(proxy)).isEqualTo(ITestBean.class);
313314

314315
ProxyFactory pf2 = new ProxyFactory(proxy);
315316
Object proxy2 = pf2.getProxy();
316317
assertThat(AopUtils.isJdkDynamicProxy(proxy2)).as("Proxy is a JDK proxy").isTrue();
317-
assertThat(proxy2 instanceof ITestBean).isTrue();
318+
assertThat(proxy2).isInstanceOf(ITestBean.class);
318319
assertThat(AopProxyUtils.ultimateTargetClass(proxy2)).isEqualTo(ITestBean.class);
319320
}
320321

@@ -324,14 +325,14 @@ public void testProxyTargetClassWithConcreteClassAsTarget() {
324325
pf.setTargetClass(TestBean.class);
325326
Object proxy = pf.getProxy();
326327
assertThat(AopUtils.isCglibProxy(proxy)).as("Proxy is a CGLIB proxy").isTrue();
327-
assertThat(proxy instanceof TestBean).isTrue();
328+
assertThat(proxy).isInstanceOf(TestBean.class);
328329
assertThat(AopProxyUtils.ultimateTargetClass(proxy)).isEqualTo(TestBean.class);
329330

330331
ProxyFactory pf2 = new ProxyFactory(proxy);
331332
pf2.setProxyTargetClass(true);
332333
Object proxy2 = pf2.getProxy();
333334
assertThat(AopUtils.isCglibProxy(proxy2)).as("Proxy is a CGLIB proxy").isTrue();
334-
assertThat(proxy2 instanceof TestBean).isTrue();
335+
assertThat(proxy2).isInstanceOf(TestBean.class);
335336
assertThat(AopProxyUtils.ultimateTargetClass(proxy2)).isEqualTo(TestBean.class);
336337
}
337338

@@ -341,8 +342,8 @@ public void testExclusionOfNonPublicInterfaces() {
341342
JFrame frame = new JFrame();
342343
ProxyFactory proxyFactory = new ProxyFactory(frame);
343344
Object proxy = proxyFactory.getProxy();
344-
assertThat(proxy instanceof RootPaneContainer).isTrue();
345-
assertThat(proxy instanceof Accessible).isTrue();
345+
assertThat(proxy).isInstanceOf(RootPaneContainer.class);
346+
assertThat(proxy).isInstanceOf(Accessible.class);
346347
}
347348

348349
@Test

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,7 @@ class InvocationCheckExposedInvocationTestBean extends ExposedInvocationTestBean
2626

2727
@Override
2828
protected void assertions(MethodInvocation invocation) {
29-
assertThat(invocation.getThis() == this).isTrue();
29+
assertThat(invocation.getThis()).isSameAs(this);
3030
assertThat(ITestBean.class.isAssignableFrom(invocation.getMethod().getDeclaringClass())).as("Invocation should be on ITestBean: " + invocation.getMethod()).isTrue();
3131
}
3232
}

spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationAotProcessorTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -128,7 +128,7 @@ void getBeanRegistrationCodeGeneratorWhenScopedProxyWithTargetBeanName() {
128128
this.beanFactory.registerBeanDefinition("test", scopedBean);
129129
compile((freshBeanFactory, compiled) -> {
130130
Object bean = freshBeanFactory.getBean("test");
131-
assertThat(bean).isNotNull().isInstanceOf(NumberHolder.class).isInstanceOf(AopInfrastructureBean.class);
131+
assertThat(bean).isInstanceOf(NumberHolder.class).isInstanceOf(AopInfrastructureBean.class);
132132
});
133133
}
134134

spring-aop/src/test/java/org/springframework/aop/support/ComposablePointcutTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -142,7 +142,7 @@ public void testEqualsAndHashCode() throws Exception {
142142
pc1.intersection(GETTER_METHOD_MATCHER);
143143

144144
assertThat(pc1.equals(pc2)).isFalse();
145-
assertThat(pc1.hashCode() == pc2.hashCode()).isFalse();
145+
assertThat(pc1.hashCode()).isNotEqualTo(pc2.hashCode());
146146

147147
pc2.intersection(GETTER_METHOD_MATCHER);
148148

spring-aop/src/test/java/org/springframework/aop/support/ControlFlowPointcutTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -97,7 +97,7 @@ public void testEqualsAndHashCode() throws Exception {
9797
assertThat(new ControlFlowPointcut(One.class, "getAge").equals(new ControlFlowPointcut(One.class))).isFalse();
9898
assertThat(new ControlFlowPointcut(One.class).hashCode()).isEqualTo(new ControlFlowPointcut(One.class).hashCode());
9999
assertThat(new ControlFlowPointcut(One.class, "getAge").hashCode()).isEqualTo(new ControlFlowPointcut(One.class, "getAge").hashCode());
100-
assertThat(new ControlFlowPointcut(One.class, "getAge").hashCode() == new ControlFlowPointcut(One.class).hashCode()).isFalse();
100+
assertThat(new ControlFlowPointcut(One.class, "getAge").hashCode()).isNotEqualTo(new ControlFlowPointcut(One.class).hashCode());
101101
}
102102

103103
@Test

0 commit comments

Comments
 (0)