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
This commit is contained in:
Phillip Webb
2019-05-23 15:51:39 -07:00
parent 95a9d46a87
commit 9d74da006c
1636 changed files with 37861 additions and 40390 deletions

View File

@@ -20,7 +20,7 @@ import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Chris Beams
@@ -33,7 +33,7 @@ public class XmlBeanConfigurerTests {
"org/springframework/beans/factory/aspectj/beanConfigurerTests.xml")) {
ShouldBeConfiguredBySpring myObject = new ShouldBeConfiguredBySpring();
assertEquals("Rod", myObject.getName());
assertThat(myObject.getName()).isEqualTo("Rod");
}
}

View File

@@ -24,9 +24,7 @@ import org.springframework.cache.config.CacheableService;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Costin Leau
@@ -43,7 +41,7 @@ public class AspectJCacheAnnotationTests extends AbstractCacheAnnotationTests {
public void testKeyStrategy() throws Exception {
AnnotationCacheAspect aspect = ctx.getBean(
"org.springframework.cache.config.internalCacheAspect", AnnotationCacheAspect.class);
assertSame(ctx.getBean("keyGenerator"), aspect.getKeyGenerator());
assertThat(aspect.getKeyGenerator()).isSameAs(ctx.getBean("keyGenerator"));
}
@Override
@@ -56,21 +54,21 @@ public class AspectJCacheAnnotationTests extends AbstractCacheAnnotationTests {
Cache primary = cm.getCache("primary");
Cache secondary = cm.getCache("secondary");
assertSame(r1, r2);
assertSame(r1, primary.get(o1).get());
assertSame(r1, secondary.get(o1).get());
assertThat(r2).isSameAs(r1);
assertThat(primary.get(o1).get()).isSameAs(r1);
assertThat(secondary.get(o1).get()).isSameAs(r1);
service.multiEvict(o1);
assertNull(primary.get(o1));
assertNull(secondary.get(o1));
assertThat(primary.get(o1)).isNull();
assertThat(secondary.get(o1)).isNull();
Object r3 = service.multiCache(o1);
Object r4 = service.multiCache(o1);
assertNotSame(r1, r3);
assertSame(r3, r4);
assertThat(r3).isNotSameAs(r1);
assertThat(r4).isSameAs(r3);
assertSame(r3, primary.get(o1).get());
assertSame(r4, secondary.get(o1).get());
assertThat(primary.get(o1).get()).isSameAs(r3);
assertThat(secondary.get(o1).get()).isSameAs(r4);
}
}

View File

@@ -43,10 +43,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Stephane Nicoll
@@ -72,14 +69,14 @@ public class AspectJEnableCachingIsolatedTests {
public void testKeyStrategy() {
load(EnableCachingConfig.class);
AnnotationCacheAspect aspect = this.ctx.getBean(AnnotationCacheAspect.class);
assertSame(this.ctx.getBean("keyGenerator", KeyGenerator.class), aspect.getKeyGenerator());
assertThat(aspect.getKeyGenerator()).isSameAs(this.ctx.getBean("keyGenerator", KeyGenerator.class));
}
@Test
public void testCacheErrorHandler() {
load(EnableCachingConfig.class);
AnnotationCacheAspect aspect = this.ctx.getBean(AnnotationCacheAspect.class);
assertSame(this.ctx.getBean("errorHandler", CacheErrorHandler.class), aspect.getErrorHandler());
assertThat(aspect.getErrorHandler()).isSameAs(this.ctx.getBean("errorHandler", CacheErrorHandler.class));
}
@@ -96,7 +93,7 @@ public class AspectJEnableCachingIsolatedTests {
load(MultiCacheManagerConfig.class);
}
catch (IllegalStateException ex) {
assertTrue(ex.getMessage().contains("bean of type CacheManager"));
assertThat(ex.getMessage().contains("bean of type CacheManager")).isTrue();
}
}
@@ -112,8 +109,9 @@ public class AspectJEnableCachingIsolatedTests {
}
catch (BeanCreationException ex) {
Throwable root = ex.getRootCause();
assertTrue(root instanceof IllegalStateException);
assertTrue(ex.getMessage().contains("implementations of CachingConfigurer"));
boolean condition = root instanceof IllegalStateException;
assertThat(condition).isTrue();
assertThat(ex.getMessage().contains("implementations of CachingConfigurer")).isTrue();
}
}
@@ -123,7 +121,7 @@ public class AspectJEnableCachingIsolatedTests {
load(EmptyConfig.class);
}
catch (IllegalStateException ex) {
assertTrue(ex.getMessage().contains("no bean of type CacheManager"));
assertThat(ex.getMessage().contains("no bean of type CacheManager")).isTrue();
}
}
@@ -132,10 +130,9 @@ public class AspectJEnableCachingIsolatedTests {
public void emptyConfigSupport() {
load(EmptyConfigSupportConfig.class);
AnnotationCacheAspect aspect = this.ctx.getBean(AnnotationCacheAspect.class);
assertNotNull(aspect.getCacheResolver());
assertEquals(SimpleCacheResolver.class, aspect.getCacheResolver().getClass());
assertSame(this.ctx.getBean(CacheManager.class),
((SimpleCacheResolver) aspect.getCacheResolver()).getCacheManager());
assertThat(aspect.getCacheResolver()).isNotNull();
assertThat(aspect.getCacheResolver().getClass()).isEqualTo(SimpleCacheResolver.class);
assertThat(((SimpleCacheResolver) aspect.getCacheResolver()).getCacheManager()).isSameAs(this.ctx.getBean(CacheManager.class));
}
@Test
@@ -143,8 +140,8 @@ public class AspectJEnableCachingIsolatedTests {
load(FullCachingConfig.class);
AnnotationCacheAspect aspect = this.ctx.getBean(AnnotationCacheAspect.class);
assertSame(this.ctx.getBean("cacheResolver"), aspect.getCacheResolver());
assertSame(this.ctx.getBean("keyGenerator"), aspect.getKeyGenerator());
assertThat(aspect.getCacheResolver()).isSameAs(this.ctx.getBean("cacheResolver"));
assertThat(aspect.getKeyGenerator()).isSameAs(this.ctx.getBean("keyGenerator"));
}

View File

@@ -23,7 +23,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests that @EnableSpringConfigured properly registers an
@@ -39,7 +39,7 @@ public class AnnotationBeanConfigurerTests {
public void injection() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class)) {
ShouldBeConfiguredBySpring myObject = new ShouldBeConfiguredBySpring();
assertEquals("Rod", myObject.getName());
assertThat(myObject.getName()).isEqualTo("Rod");
}
}

View File

@@ -39,8 +39,6 @@ import org.springframework.util.ReflectionUtils;
import org.springframework.util.concurrent.ListenableFuture;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
/**
* Unit tests for {@link AnnotationAsyncExecutionAspect}.
@@ -71,9 +69,9 @@ public class AnnotationAsyncExecutionAspectTests {
ClassWithoutAsyncAnnotation obj = new ClassWithoutAsyncAnnotation();
obj.incrementAsync();
executor.waitForCompletion();
assertEquals(1, obj.counter);
assertEquals(1, executor.submitStartCounter);
assertEquals(1, executor.submitCompleteCounter);
assertThat(obj.counter).isEqualTo(1);
assertThat(executor.submitStartCounter).isEqualTo(1);
assertThat(executor.submitCompleteCounter).isEqualTo(1);
}
@Test
@@ -81,19 +79,19 @@ public class AnnotationAsyncExecutionAspectTests {
ClassWithoutAsyncAnnotation obj = new ClassWithoutAsyncAnnotation();
Future<Integer> future = obj.incrementReturningAFuture();
// No need to executor.waitForCompletion() as future.get() will have the same effect
assertEquals(5, future.get().intValue());
assertEquals(1, obj.counter);
assertEquals(1, executor.submitStartCounter);
assertEquals(1, executor.submitCompleteCounter);
assertThat(future.get().intValue()).isEqualTo(5);
assertThat(obj.counter).isEqualTo(1);
assertThat(executor.submitStartCounter).isEqualTo(1);
assertThat(executor.submitCompleteCounter).isEqualTo(1);
}
@Test
public void syncMethodGetsRoutedSynchronously() {
ClassWithoutAsyncAnnotation obj = new ClassWithoutAsyncAnnotation();
obj.increment();
assertEquals(1, obj.counter);
assertEquals(0, executor.submitStartCounter);
assertEquals(0, executor.submitCompleteCounter);
assertThat(obj.counter).isEqualTo(1);
assertThat(executor.submitStartCounter).isEqualTo(0);
assertThat(executor.submitCompleteCounter).isEqualTo(0);
}
@Test
@@ -103,19 +101,19 @@ public class AnnotationAsyncExecutionAspectTests {
ClassWithAsyncAnnotation obj = new ClassWithAsyncAnnotation();
obj.increment();
executor.waitForCompletion();
assertEquals(1, obj.counter);
assertEquals(1, executor.submitStartCounter);
assertEquals(1, executor.submitCompleteCounter);
assertThat(obj.counter).isEqualTo(1);
assertThat(executor.submitStartCounter).isEqualTo(1);
assertThat(executor.submitCompleteCounter).isEqualTo(1);
}
@Test
public void methodReturningFutureInAsyncClassGetsRoutedAsynchronouslyAndReturnsAFuture() throws InterruptedException, ExecutionException {
ClassWithAsyncAnnotation obj = new ClassWithAsyncAnnotation();
Future<Integer> future = obj.incrementReturningAFuture();
assertEquals(5, future.get().intValue());
assertEquals(1, obj.counter);
assertEquals(1, executor.submitStartCounter);
assertEquals(1, executor.submitCompleteCounter);
assertThat(future.get().intValue()).isEqualTo(5);
assertThat(obj.counter).isEqualTo(1);
assertThat(executor.submitStartCounter).isEqualTo(1);
assertThat(executor.submitCompleteCounter).isEqualTo(1);
}
/*
@@ -154,7 +152,7 @@ public class AnnotationAsyncExecutionAspectTests {
TestableAsyncUncaughtExceptionHandler exceptionHandler = new TestableAsyncUncaughtExceptionHandler();
AnnotationAsyncExecutionAspect.aspectOf().setExceptionHandler(exceptionHandler);
try {
assertFalse("Handler should not have been called", exceptionHandler.isCalled());
assertThat(exceptionHandler.isCalled()).as("Handler should not have been called").isFalse();
ClassWithException obj = new ClassWithException();
obj.failWithVoid();
exceptionHandler.await(3000);
@@ -171,7 +169,7 @@ public class AnnotationAsyncExecutionAspectTests {
TestableAsyncUncaughtExceptionHandler exceptionHandler = new TestableAsyncUncaughtExceptionHandler(true);
AnnotationAsyncExecutionAspect.aspectOf().setExceptionHandler(exceptionHandler);
try {
assertFalse("Handler should not have been called", exceptionHandler.isCalled());
assertThat(exceptionHandler.isCalled()).as("Handler should not have been called").isFalse();
ClassWithException obj = new ClassWithException();
obj.failWithVoid();
exceptionHandler.await(3000);

View File

@@ -27,8 +27,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.config.TaskManagementConfigUtils;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Stephane Nicoll
@@ -52,7 +51,7 @@ public class AnnotationDrivenBeanDefinitionParserTests {
@Test
public void asyncAspectRegistered() {
assertTrue(context.containsBean(TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME));
assertThat(context.containsBean(TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME)).isTrue();
}
@Test
@@ -60,7 +59,7 @@ public class AnnotationDrivenBeanDefinitionParserTests {
public void asyncPostProcessorExecutorReference() {
Object executor = context.getBean("testExecutor");
Object aspect = context.getBean(TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME);
assertSame(executor, ((Supplier) new DirectFieldAccessor(aspect).getPropertyValue("defaultExecutor")).get());
assertThat(((Supplier) new DirectFieldAccessor(aspect).getPropertyValue("defaultExecutor")).get()).isSameAs(executor);
}
@Test
@@ -68,7 +67,7 @@ public class AnnotationDrivenBeanDefinitionParserTests {
public void asyncPostProcessorExceptionHandlerReference() {
Object exceptionHandler = context.getBean("testExceptionHandler");
Object aspect = context.getBean(TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME);
assertSame(exceptionHandler, ((Supplier) new DirectFieldAccessor(aspect).getPropertyValue("exceptionHandler")).get());
assertThat(((Supplier) new DirectFieldAccessor(aspect).getPropertyValue("exceptionHandler")).get()).isSameAs(exceptionHandler);
}
}

View File

@@ -22,8 +22,7 @@ import java.util.concurrent.TimeUnit;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;
/**
* A {@link AsyncUncaughtExceptionHandler} implementation used for testing purposes.
@@ -61,9 +60,9 @@ class TestableAsyncUncaughtExceptionHandler
}
public void assertCalledWith(Method expectedMethod, Class<? extends Throwable> expectedExceptionType) {
assertNotNull("Handler not called", descriptor);
assertEquals("Wrong exception type", expectedExceptionType, descriptor.ex.getClass());
assertEquals("Wrong method", expectedMethod, descriptor.method);
assertThat(descriptor).as("Handler not called").isNotNull();
assertThat(descriptor.ex.getClass()).as("Wrong exception type").isEqualTo(expectedExceptionType);
assertThat(descriptor.method).as("Wrong method").isEqualTo(expectedMethod);
}
public void await(long timeout) {

View File

@@ -30,9 +30,9 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.tests.transaction.CallCountingTransactionManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIOException;
import static org.junit.Assert.assertEquals;
/**
* @author Stephane Nicoll
@@ -51,66 +51,66 @@ public class JtaTransactionAspectsTests {
@Test
public void commitOnAnnotatedPublicMethod() throws Throwable {
assertEquals(0, this.txManager.begun);
assertThat(this.txManager.begun).isEqualTo(0);
new JtaAnnotationPublicAnnotatedMember().echo(null);
assertEquals(1, this.txManager.commits);
assertThat(this.txManager.commits).isEqualTo(1);
}
@Test
public void matchingRollbackOnApplied() throws Throwable {
assertEquals(0, this.txManager.begun);
assertThat(this.txManager.begun).isEqualTo(0);
InterruptedException test = new InterruptedException();
assertThatExceptionOfType(InterruptedException.class).isThrownBy(() ->
new JtaAnnotationPublicAnnotatedMember().echo(test))
.isSameAs(test);
assertEquals(1, this.txManager.rollbacks);
assertEquals(0, this.txManager.commits);
assertThat(this.txManager.rollbacks).isEqualTo(1);
assertThat(this.txManager.commits).isEqualTo(0);
}
@Test
public void nonMatchingRollbackOnApplied() throws Throwable {
assertEquals(0, this.txManager.begun);
assertThat(this.txManager.begun).isEqualTo(0);
IOException test = new IOException();
assertThatIOException().isThrownBy(() ->
new JtaAnnotationPublicAnnotatedMember().echo(test))
.isSameAs(test);
assertEquals(1, this.txManager.commits);
assertEquals(0, this.txManager.rollbacks);
assertThat(this.txManager.commits).isEqualTo(1);
assertThat(this.txManager.rollbacks).isEqualTo(0);
}
@Test
public void commitOnAnnotatedProtectedMethod() {
assertEquals(0, this.txManager.begun);
assertThat(this.txManager.begun).isEqualTo(0);
new JtaAnnotationProtectedAnnotatedMember().doInTransaction();
assertEquals(1, this.txManager.commits);
assertThat(this.txManager.commits).isEqualTo(1);
}
@Test
public void nonAnnotatedMethodCallingProtectedMethod() {
assertEquals(0, this.txManager.begun);
assertThat(this.txManager.begun).isEqualTo(0);
new JtaAnnotationProtectedAnnotatedMember().doSomething();
assertEquals(1, this.txManager.commits);
assertThat(this.txManager.commits).isEqualTo(1);
}
@Test
public void commitOnAnnotatedPrivateMethod() {
assertEquals(0, this.txManager.begun);
assertThat(this.txManager.begun).isEqualTo(0);
new JtaAnnotationPrivateAnnotatedMember().doInTransaction();
assertEquals(1, this.txManager.commits);
assertThat(this.txManager.commits).isEqualTo(1);
}
@Test
public void nonAnnotatedMethodCallingPrivateMethod() {
assertEquals(0, this.txManager.begun);
assertThat(this.txManager.begun).isEqualTo(0);
new JtaAnnotationPrivateAnnotatedMember().doSomething();
assertEquals(1, this.txManager.commits);
assertThat(this.txManager.commits).isEqualTo(1);
}
@Test
public void notTransactional() {
assertEquals(0, this.txManager.begun);
assertThat(this.txManager.begun).isEqualTo(0);
new TransactionAspectTests.NotTransactional().noop();
assertEquals(0, this.txManager.begun);
assertThat(this.txManager.begun).isEqualTo(0);
}

View File

@@ -21,8 +21,8 @@ import org.junit.Test;
import org.springframework.tests.transaction.CallCountingTransactionManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
/**
* @author Rod Johnson
@@ -56,49 +56,49 @@ public class TransactionAspectTests {
@Test
public void testCommitOnAnnotatedClass() throws Throwable {
txManager.clear();
assertEquals(0, txManager.begun);
assertThat(txManager.begun).isEqualTo(0);
annotationOnlyOnClassWithNoInterface.echo(null);
assertEquals(1, txManager.commits);
assertThat(txManager.commits).isEqualTo(1);
}
@Test
public void commitOnAnnotatedProtectedMethod() throws Throwable {
txManager.clear();
assertEquals(0, txManager.begun);
assertThat(txManager.begun).isEqualTo(0);
beanWithAnnotatedProtectedMethod.doInTransaction();
assertEquals(1, txManager.commits);
assertThat(txManager.commits).isEqualTo(1);
}
@Test
public void commitOnAnnotatedPrivateMethod() throws Throwable {
txManager.clear();
assertEquals(0, txManager.begun);
assertThat(txManager.begun).isEqualTo(0);
beanWithAnnotatedPrivateMethod.doSomething();
assertEquals(1, txManager.commits);
assertThat(txManager.commits).isEqualTo(1);
}
@Test
public void commitOnNonAnnotatedNonPublicMethodInTransactionalType() throws Throwable {
txManager.clear();
assertEquals(0, txManager.begun);
assertThat(txManager.begun).isEqualTo(0);
annotationOnlyOnClassWithNoInterface.nonTransactionalMethod();
assertEquals(0, txManager.begun);
assertThat(txManager.begun).isEqualTo(0);
}
@Test
public void commitOnAnnotatedMethod() throws Throwable {
txManager.clear();
assertEquals(0, txManager.begun);
assertThat(txManager.begun).isEqualTo(0);
methodAnnotationOnly.echo(null);
assertEquals(1, txManager.commits);
assertThat(txManager.commits).isEqualTo(1);
}
@Test
public void notTransactional() throws Throwable {
txManager.clear();
assertEquals(0, txManager.begun);
assertThat(txManager.begun).isEqualTo(0);
new NotTransactional().noop();
assertEquals(0, txManager.begun);
assertThat(txManager.begun).isEqualTo(0);
}
@Test
@@ -149,23 +149,25 @@ public class TransactionAspectTests {
protected void testRollback(TransactionOperationCallback toc, boolean rollback) throws Throwable {
txManager.clear();
assertEquals(0, txManager.begun);
assertThat(txManager.begun).isEqualTo(0);
try {
toc.performTransactionalOperation();
}
finally {
assertEquals(1, txManager.begun);
assertEquals(rollback ? 0 : 1, txManager.commits);
assertEquals(rollback ? 1 : 0, txManager.rollbacks);
assertThat(txManager.begun).isEqualTo(1);
long expected1 = rollback ? 0 : 1;
assertThat(txManager.commits).isEqualTo(expected1);
long expected = rollback ? 1 : 0;
assertThat(txManager.rollbacks).isEqualTo(expected);
}
}
protected void testNotTransactional(TransactionOperationCallback toc, Throwable expected) throws Throwable {
txManager.clear();
assertEquals(0, txManager.begun);
assertThat(txManager.begun).isEqualTo(0);
assertThatExceptionOfType(Throwable.class).isThrownBy(
toc::performTransactionalOperation).isSameAs(expected);
assertEquals(0, txManager.begun);
assertThat(txManager.begun).isEqualTo(0);
}