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
This commit is contained in:
Krzysztof Krasoń
2023-04-04 17:34:07 +02:00
committed by GitHub
parent dd97ee4e99
commit 1734deca1e
371 changed files with 3177 additions and 3076 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -70,7 +70,7 @@ public class MethodInvocationProceedingJoinPointTests {
AtomicInteger depth = new AtomicInteger();
pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
assertThat(jp.toString().contains(method.getName())).as("Method named in toString").isTrue();
assertThat(jp.toString()).as("Method named in toString").contains(method.getName());
// Ensure that these don't cause problems
jp.toShortString();
jp.toLongString();

View File

@@ -319,10 +319,10 @@ abstract class AbstractAspectJAdvisorFactoryTests {
@Test
void introductionOnTargetNotImplementingInterface() {
NotLockable notLockableTarget = new NotLockable();
assertThat(notLockableTarget instanceof Lockable).isFalse();
assertThat(notLockableTarget).isNotInstanceOf(Lockable.class);
NotLockable notLockable1 = createProxy(notLockableTarget, NotLockable.class,
getAdvisorFactory().getAdvisors(aspectInstanceFactory(new MakeLockable(), "someBean")));
assertThat(notLockable1 instanceof Lockable).isTrue();
assertThat(notLockable1).isInstanceOf(Lockable.class);
Lockable lockable = (Lockable) notLockable1;
assertThat(lockable.locked()).isFalse();
lockable.lock();
@@ -331,7 +331,7 @@ abstract class AbstractAspectJAdvisorFactoryTests {
NotLockable notLockable2Target = new NotLockable();
NotLockable notLockable2 = createProxy(notLockable2Target, NotLockable.class,
getAdvisorFactory().getAdvisors(aspectInstanceFactory(new MakeLockable(), "someBean")));
assertThat(notLockable2 instanceof Lockable).isTrue();
assertThat(notLockable2).isInstanceOf(Lockable.class);
Lockable lockable2 = (Lockable) notLockable2;
assertThat(lockable2.locked()).isFalse();
notLockable2.setIntValue(1);
@@ -345,7 +345,7 @@ abstract class AbstractAspectJAdvisorFactoryTests {
assertThat(AopUtils.findAdvisorsThatCanApply(
getAdvisorFactory().getAdvisors(
aspectInstanceFactory(new MakeLockable(), "someBean")),
CannotBeUnlocked.class).isEmpty()).isTrue();
CannotBeUnlocked.class)).isEmpty();
assertThat(AopUtils.findAdvisorsThatCanApply(getAdvisorFactory().getAdvisors(
aspectInstanceFactory(new MakeLockable(),"someBean")), NotLockable.class)).hasSize(2);
}
@@ -373,7 +373,7 @@ abstract class AbstractAspectJAdvisorFactoryTests {
AopUtils.findAdvisorsThatCanApply(
getAdvisorFactory().getAdvisors(aspectInstanceFactory(new MakeLockable(), "someBean")),
List.class));
assertThat(proxy instanceof Lockable).as("Type pattern must have excluded mixin").isFalse();
assertThat(proxy).as("Type pattern must have excluded mixin").isNotInstanceOf(Lockable.class);
}
@Test
@@ -430,7 +430,7 @@ abstract class AbstractAspectJAdvisorFactoryTests {
UnsupportedOperationException expectedException = new UnsupportedOperationException();
List<Advisor> advisors = getAdvisorFactory().getAdvisors(
aspectInstanceFactory(new ExceptionThrowingAspect(expectedException), "someBean"));
assertThat(advisors.size()).as("One advice method was found").isEqualTo(1);
assertThat(advisors).as("One advice method was found").hasSize(1);
ITestBean itb = createProxy(target, ITestBean.class, advisors);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(itb::getAge);
}
@@ -443,7 +443,7 @@ abstract class AbstractAspectJAdvisorFactoryTests {
RemoteException expectedException = new RemoteException();
List<Advisor> advisors = getAdvisorFactory().getAdvisors(
aspectInstanceFactory(new ExceptionThrowingAspect(expectedException), "someBean"));
assertThat(advisors.size()).as("One advice method was found").isEqualTo(1);
assertThat(advisors).as("One advice method was found").hasSize(1);
ITestBean itb = createProxy(target, ITestBean.class, advisors);
assertThatExceptionOfType(UndeclaredThrowableException.class)
.isThrownBy(itb::getAge)
@@ -456,7 +456,7 @@ abstract class AbstractAspectJAdvisorFactoryTests {
TwoAdviceAspect twoAdviceAspect = new TwoAdviceAspect();
List<Advisor> advisors = getAdvisorFactory().getAdvisors(
aspectInstanceFactory(twoAdviceAspect, "someBean"));
assertThat(advisors.size()).as("Two advice methods found").isEqualTo(2);
assertThat(advisors).as("Two advice methods found").hasSize(2);
ITestBean itb = createProxy(target, ITestBean.class, advisors);
itb.setName("");
assertThat(itb.getAge()).isEqualTo(0);

View File

@@ -197,11 +197,11 @@ public class ProxyFactoryTests {
TestBeanSubclass raw = new TestBeanSubclass();
ProxyFactory factory = new ProxyFactory(raw);
//System.out.println("Proxied interfaces are " + StringUtils.arrayToDelimitedString(factory.getProxiedInterfaces(), ","));
assertThat(factory.getProxiedInterfaces().length).as("Found correct number of interfaces").isEqualTo(5);
assertThat(factory.getProxiedInterfaces()).as("Found correct number of interfaces").hasSize(5);
ITestBean tb = (ITestBean) factory.getProxy();
assertThat(tb).as("Picked up secondary interface").isInstanceOf(IOther.class);
raw.setAge(25);
assertThat(tb.getAge() == raw.getAge()).isTrue();
assertThat(tb.getAge()).isEqualTo(raw.getAge());
long t = 555555L;
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t);
@@ -211,10 +211,10 @@ public class ProxyFactoryTests {
factory.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
Class<?>[] newProxiedInterfaces = factory.getProxiedInterfaces();
assertThat(newProxiedInterfaces.length).as("Advisor proxies one more interface after introduction").isEqualTo(oldProxiedInterfaces.length + 1);
assertThat(newProxiedInterfaces).as("Advisor proxies one more interface after introduction").hasSize(oldProxiedInterfaces.length + 1);
TimeStamped ts = (TimeStamped) factory.getProxy();
assertThat(ts.getTimeStamp() == t).isTrue();
assertThat(ts.getTimeStamp()).isEqualTo(t);
// Shouldn't fail;
((IOther) ts).absquatulate();
}
@@ -234,13 +234,13 @@ public class ProxyFactoryTests {
factory.addAdvice(0, di);
assertThat(factory.getProxy()).isInstanceOf(ITestBean.class);
assertThat(factory.adviceIncluded(di)).isTrue();
assertThat(!factory.adviceIncluded(diUnused)).isTrue();
assertThat(factory.countAdvicesOfType(NopInterceptor.class) == 1).isTrue();
assertThat(factory.countAdvicesOfType(MyInterceptor.class) == 0).isTrue();
assertThat(factory.adviceIncluded(diUnused)).isFalse();
assertThat(factory.countAdvicesOfType(NopInterceptor.class)).isEqualTo(1);
assertThat(factory.countAdvicesOfType(MyInterceptor.class)).isEqualTo(0);
factory.addAdvice(0, diUnused);
assertThat(factory.adviceIncluded(diUnused)).isTrue();
assertThat(factory.countAdvicesOfType(NopInterceptor.class) == 2).isTrue();
assertThat(factory.countAdvicesOfType(NopInterceptor.class)).isEqualTo(2);
}
@Test
@@ -260,7 +260,8 @@ public class ProxyFactoryTests {
public void testCanAddAndRemoveAspectInterfacesOnSingleton() {
ProxyFactory config = new ProxyFactory(new TestBean());
assertThat(config.getProxy() instanceof TimeStamped).as("Shouldn't implement TimeStamped before manipulation").isFalse();
assertThat(config.getProxy()).as("Shouldn't implement TimeStamped before manipulation")
.isNotInstanceOf(TimeStamped.class);
long time = 666L;
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
@@ -270,26 +271,26 @@ public class ProxyFactoryTests {
int oldCount = config.getAdvisors().length;
config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
assertThat(config.getAdvisors().length == oldCount + 1).isTrue();
assertThat(config.getAdvisors()).hasSize(oldCount + 1);
TimeStamped ts = (TimeStamped) config.getProxy();
assertThat(ts.getTimeStamp() == time).isTrue();
assertThat(ts.getTimeStamp()).isEqualTo(time);
// Can remove
config.removeAdvice(ti);
assertThat(config.getAdvisors().length == oldCount).isTrue();
assertThat(config.getAdvisors()).hasSize(oldCount);
assertThatRuntimeException()
.as("Existing object won't implement this interface any more")
.isThrownBy(ts::getTimeStamp); // Existing reference will fail
assertThat(config.getProxy() instanceof TimeStamped).as("Should no longer implement TimeStamped").isFalse();
assertThat(config.getProxy()).as("Should no longer implement TimeStamped").isNotInstanceOf(TimeStamped.class);
// Now check non-effect of removing interceptor that isn't there
config.removeAdvice(new DebugInterceptor());
assertThat(config.getAdvisors().length == oldCount).isTrue();
assertThat(config.getAdvisors()).hasSize(oldCount);
ITestBean it = (ITestBean) ts;
DebugInterceptor debugInterceptor = new DebugInterceptor();
@@ -299,7 +300,7 @@ public class ProxyFactoryTests {
config.removeAdvice(debugInterceptor);
it.getSpouse();
// not invoked again
assertThat(debugInterceptor.getCount() == 1).isTrue();
assertThat(debugInterceptor.getCount()).isEqualTo(1);
}
@Test
@@ -308,13 +309,13 @@ public class ProxyFactoryTests {
pf.setTargetClass(ITestBean.class);
Object proxy = pf.getProxy();
assertThat(AopUtils.isJdkDynamicProxy(proxy)).as("Proxy is a JDK proxy").isTrue();
assertThat(proxy instanceof ITestBean).isTrue();
assertThat(proxy).isInstanceOf(ITestBean.class);
assertThat(AopProxyUtils.ultimateTargetClass(proxy)).isEqualTo(ITestBean.class);
ProxyFactory pf2 = new ProxyFactory(proxy);
Object proxy2 = pf2.getProxy();
assertThat(AopUtils.isJdkDynamicProxy(proxy2)).as("Proxy is a JDK proxy").isTrue();
assertThat(proxy2 instanceof ITestBean).isTrue();
assertThat(proxy2).isInstanceOf(ITestBean.class);
assertThat(AopProxyUtils.ultimateTargetClass(proxy2)).isEqualTo(ITestBean.class);
}
@@ -324,14 +325,14 @@ public class ProxyFactoryTests {
pf.setTargetClass(TestBean.class);
Object proxy = pf.getProxy();
assertThat(AopUtils.isCglibProxy(proxy)).as("Proxy is a CGLIB proxy").isTrue();
assertThat(proxy instanceof TestBean).isTrue();
assertThat(proxy).isInstanceOf(TestBean.class);
assertThat(AopProxyUtils.ultimateTargetClass(proxy)).isEqualTo(TestBean.class);
ProxyFactory pf2 = new ProxyFactory(proxy);
pf2.setProxyTargetClass(true);
Object proxy2 = pf2.getProxy();
assertThat(AopUtils.isCglibProxy(proxy2)).as("Proxy is a CGLIB proxy").isTrue();
assertThat(proxy2 instanceof TestBean).isTrue();
assertThat(proxy2).isInstanceOf(TestBean.class);
assertThat(AopProxyUtils.ultimateTargetClass(proxy2)).isEqualTo(TestBean.class);
}
@@ -341,8 +342,8 @@ public class ProxyFactoryTests {
JFrame frame = new JFrame();
ProxyFactory proxyFactory = new ProxyFactory(frame);
Object proxy = proxyFactory.getProxy();
assertThat(proxy instanceof RootPaneContainer).isTrue();
assertThat(proxy instanceof Accessible).isTrue();
assertThat(proxy).isInstanceOf(RootPaneContainer.class);
assertThat(proxy).isInstanceOf(Accessible.class);
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,7 +26,7 @@ class InvocationCheckExposedInvocationTestBean extends ExposedInvocationTestBean
@Override
protected void assertions(MethodInvocation invocation) {
assertThat(invocation.getThis() == this).isTrue();
assertThat(invocation.getThis()).isSameAs(this);
assertThat(ITestBean.class.isAssignableFrom(invocation.getMethod().getDeclaringClass())).as("Invocation should be on ITestBean: " + invocation.getMethod()).isTrue();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -128,7 +128,7 @@ class ScopedProxyBeanRegistrationAotProcessorTests {
this.beanFactory.registerBeanDefinition("test", scopedBean);
compile((freshBeanFactory, compiled) -> {
Object bean = freshBeanFactory.getBean("test");
assertThat(bean).isNotNull().isInstanceOf(NumberHolder.class).isInstanceOf(AopInfrastructureBean.class);
assertThat(bean).isInstanceOf(NumberHolder.class).isInstanceOf(AopInfrastructureBean.class);
});
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -142,7 +142,7 @@ public class ComposablePointcutTests {
pc1.intersection(GETTER_METHOD_MATCHER);
assertThat(pc1.equals(pc2)).isFalse();
assertThat(pc1.hashCode() == pc2.hashCode()).isFalse();
assertThat(pc1.hashCode()).isNotEqualTo(pc2.hashCode());
pc2.intersection(GETTER_METHOD_MATCHER);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -97,7 +97,7 @@ public class ControlFlowPointcutTests {
assertThat(new ControlFlowPointcut(One.class, "getAge").equals(new ControlFlowPointcut(One.class))).isFalse();
assertThat(new ControlFlowPointcut(One.class).hashCode()).isEqualTo(new ControlFlowPointcut(One.class).hashCode());
assertThat(new ControlFlowPointcut(One.class, "getAge").hashCode()).isEqualTo(new ControlFlowPointcut(One.class, "getAge").hashCode());
assertThat(new ControlFlowPointcut(One.class, "getAge").hashCode() == new ControlFlowPointcut(One.class).hashCode()).isFalse();
assertThat(new ControlFlowPointcut(One.class, "getAge").hashCode()).isNotEqualTo(new ControlFlowPointcut(One.class).hashCode());
}
@Test

View File

@@ -56,7 +56,7 @@ class DelegatingIntroductionInterceptorTests {
@Test
void testIntroductionInterceptorWithDelegation() throws Exception {
TestBean raw = new TestBean();
assertThat(! (raw instanceof TimeStamped)).isTrue();
assertThat(raw).isNotInstanceOf(TimeStamped.class);
ProxyFactory factory = new ProxyFactory(raw);
TimeStamped ts = mock();
@@ -66,13 +66,13 @@ class DelegatingIntroductionInterceptorTests {
factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
TimeStamped tsp = (TimeStamped) factory.getProxy();
assertThat(tsp.getTimeStamp() == timestamp).isTrue();
assertThat(tsp.getTimeStamp()).isEqualTo(timestamp);
}
@Test
void testIntroductionInterceptorWithInterfaceHierarchy() throws Exception {
TestBean raw = new TestBean();
assertThat(! (raw instanceof SubTimeStamped)).isTrue();
assertThat(raw).isNotInstanceOf(SubTimeStamped.class);
ProxyFactory factory = new ProxyFactory(raw);
SubTimeStamped ts = mock();
@@ -82,13 +82,13 @@ class DelegatingIntroductionInterceptorTests {
factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), SubTimeStamped.class));
SubTimeStamped tsp = (SubTimeStamped) factory.getProxy();
assertThat(tsp.getTimeStamp() == timestamp).isTrue();
assertThat(tsp.getTimeStamp()).isEqualTo(timestamp);
}
@Test
void testIntroductionInterceptorWithSuperInterface() throws Exception {
TestBean raw = new TestBean();
assertThat(! (raw instanceof TimeStamped)).isTrue();
assertThat(raw).isNotInstanceOf(TimeStamped.class);
ProxyFactory factory = new ProxyFactory(raw);
SubTimeStamped ts = mock();
@@ -98,8 +98,8 @@ class DelegatingIntroductionInterceptorTests {
factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), TimeStamped.class));
TimeStamped tsp = (TimeStamped) factory.getProxy();
assertThat(!(tsp instanceof SubTimeStamped)).isTrue();
assertThat(tsp.getTimeStamp() == timestamp).isTrue();
assertThat(tsp).isNotInstanceOf(SubTimeStamped.class);
assertThat(tsp.getTimeStamp()).isEqualTo(timestamp);
}
@Test
@@ -125,7 +125,7 @@ class DelegatingIntroductionInterceptorTests {
//assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
TimeStamped ts = (TimeStamped) pf.getProxy();
assertThat(ts.getTimeStamp() == t).isTrue();
assertThat(ts.getTimeStamp()).isEqualTo(t);
((ITester) ts).foo();
((ITestBean) ts).getAge();
@@ -160,10 +160,10 @@ class DelegatingIntroductionInterceptorTests {
assertThat(ts).isInstanceOf(TimeStamped.class);
// Shouldn't proxy framework interfaces
assertThat(!(ts instanceof MethodInterceptor)).isTrue();
assertThat(!(ts instanceof IntroductionInterceptor)).isTrue();
assertThat(ts).isNotInstanceOf(MethodInterceptor.class);
assertThat(ts).isNotInstanceOf(IntroductionInterceptor.class);
assertThat(ts.getTimeStamp() == t).isTrue();
assertThat(ts.getTimeStamp()).isEqualTo(t);
((ITester) ts).foo();
((ITestBean) ts).getAge();
@@ -174,14 +174,14 @@ class DelegatingIntroductionInterceptorTests {
pf = new ProxyFactory(target);
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
Object o = pf.getProxy();
assertThat(!(o instanceof TimeStamped)).isTrue();
assertThat(o).isNotInstanceOf(TimeStamped.class);
}
@SuppressWarnings("serial")
@Test
void testIntroductionInterceptorDoesntReplaceToString() throws Exception {
TestBean raw = new TestBean();
assertThat(! (raw instanceof TimeStamped)).isTrue();
assertThat(raw).isNotInstanceOf(TimeStamped.class);
ProxyFactory factory = new ProxyFactory(raw);
TimeStamped ts = new SerializableTimeStamped(0);
@@ -266,7 +266,7 @@ class DelegatingIntroductionInterceptorTests {
TimeStamped ts = (TimeStamped) pf.getProxy();
// From introduction interceptor, not target
assertThat(ts.getTimeStamp() == t).isTrue();
assertThat(ts.getTimeStamp()).isEqualTo(t);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -129,7 +129,7 @@ public class NameMatchMethodPointcutTests {
pc1.setMappedName(foo);
assertThat(pc1.equals(pc2)).isFalse();
assertThat(pc1.hashCode() != pc2.hashCode()).isTrue();
assertThat(pc1.hashCode()).isNotEqualTo(pc2.hashCode());
pc2.setMappedName(foo);
assertThat(pc2).isEqualTo(pc1);