Migrate exception checking tests to use AssertJ

Migrate tests that use `@Test(expectedException=...)` or
`try...fail...catch` to use AssertJ's `assertThatException`
instead.
This commit is contained in:
Phillip Webb
2019-05-20 10:34:51 -07:00
parent fb26fc3f94
commit 02850f357f
561 changed files with 6592 additions and 10389 deletions

View File

@@ -43,7 +43,6 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
/**
* Unit tests for {@link AnnotationAsyncExecutionAspect}.
@@ -176,18 +175,12 @@ public class AnnotationAsyncExecutionAspectTests {
try {
assertFalse("Handler should not have been called", exceptionHandler.isCalled());
ClassWithException obj = new ClassWithException();
try {
obj.failWithVoid();
exceptionHandler.await(3000);
exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class);
}
catch (Exception ex) {
fail("No unexpected exception should have been received but got " + ex.getMessage());
}
obj.failWithVoid();
exceptionHandler.await(3000);
exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class);
}
finally {
AnnotationAsyncExecutionAspect.aspectOf().setExceptionHandler(defaultExceptionHandler);
}
}
@@ -215,7 +208,7 @@ public class AnnotationAsyncExecutionAspectTests {
wait(WAIT_TIME);
}
catch (InterruptedException ex) {
fail("Didn't finish the async job in " + WAIT_TIME + " milliseconds");
throw new AssertionError("Didn't finish the async job in " + WAIT_TIME + " milliseconds");
}
}
}

View File

@@ -30,8 +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.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* @author Stephane Nicoll
@@ -59,13 +60,9 @@ public class JtaTransactionAspectsTests {
public void matchingRollbackOnApplied() throws Throwable {
assertEquals(0, this.txManager.begun);
InterruptedException test = new InterruptedException();
try {
new JtaAnnotationPublicAnnotatedMember().echo(test);
fail("Should have thrown an exception");
}
catch (Throwable throwable) {
assertEquals("wrong exception", test, throwable);
}
assertThatExceptionOfType(InterruptedException.class).isThrownBy(() ->
new JtaAnnotationPublicAnnotatedMember().echo(test))
.isSameAs(test);
assertEquals(1, this.txManager.rollbacks);
assertEquals(0, this.txManager.commits);
}
@@ -74,13 +71,9 @@ public class JtaTransactionAspectsTests {
public void nonMatchingRollbackOnApplied() throws Throwable {
assertEquals(0, this.txManager.begun);
IOException test = new IOException();
try {
new JtaAnnotationPublicAnnotatedMember().echo(test);
fail("Should have thrown an exception");
}
catch (Throwable throwable) {
assertEquals("wrong exception", test, throwable);
}
assertThatIOException().isThrownBy(() ->
new JtaAnnotationPublicAnnotatedMember().echo(test))
.isSameAs(test);
assertEquals(1, this.txManager.commits);
assertEquals(0, this.txManager.rollbacks);
}

View File

@@ -21,9 +21,8 @@ import org.junit.Test;
import org.springframework.tests.transaction.CallCountingTransactionManager;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
/**
* @author Rod Johnson
@@ -104,50 +103,34 @@ public class TransactionAspectTests {
@Test
public void defaultCommitOnAnnotatedClass() throws Throwable {
final Exception ex = new Exception();
try {
testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), false);
fail("Should have thrown Exception");
}
catch (Exception ex2) {
assertSame(ex, ex2);
}
Exception ex = new Exception();
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), false))
.isSameAs(ex);
}
@Test
public void defaultRollbackOnAnnotatedClass() throws Throwable {
final RuntimeException ex = new RuntimeException();
try {
testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), true);
fail("Should have thrown RuntimeException");
}
catch (RuntimeException ex2) {
assertSame(ex, ex2);
}
RuntimeException ex = new RuntimeException();
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), true))
.isSameAs(ex);
}
@Test
public void defaultCommitOnSubclassOfAnnotatedClass() throws Throwable {
final Exception ex = new Exception();
try {
testRollback(() -> new SubclassOfClassWithTransactionalAnnotation().echo(ex), false);
fail("Should have thrown Exception");
}
catch (Exception ex2) {
assertSame(ex, ex2);
}
Exception ex = new Exception();
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
testRollback(() -> new SubclassOfClassWithTransactionalAnnotation().echo(ex), false))
.isSameAs(ex);
}
@Test
public void defaultCommitOnSubclassOfClassWithTransactionalMethodAnnotated() throws Throwable {
final Exception ex = new Exception();
try {
testRollback(() -> new SubclassOfClassWithTransactionalMethodAnnotation().echo(ex), false);
fail("Should have thrown Exception");
}
catch (Exception ex2) {
assertSame(ex, ex2);
}
Exception ex = new Exception();
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
testRollback(() -> new SubclassOfClassWithTransactionalMethodAnnotation().echo(ex), false))
.isSameAs(ex);
}
@Test
@@ -180,18 +163,9 @@ public class TransactionAspectTests {
protected void testNotTransactional(TransactionOperationCallback toc, Throwable expected) throws Throwable {
txManager.clear();
assertEquals(0, txManager.begun);
try {
toc.performTransactionalOperation();
}
catch (Throwable ex) {
if (expected == null) {
fail("Expected " + expected);
}
assertSame(expected, ex);
}
finally {
assertEquals(0, txManager.begun);
}
assertThatExceptionOfType(Throwable.class).isThrownBy(
toc::performTransactionalOperation).isSameAs(expected);
assertEquals(0, txManager.begun);
}