Migrate away from ExpectedException (#22922)

* Add limited checkstyles to test code

Add a limited set of checkstyle rules to the test codebase to improve
code consistency.

* Fix checksyle violations in test code

* Organize imports to fix checkstyle for test code

* Migrate to assertThatExceptionOfType

Migrate aware from ExpectedException rules to AssertJ exception
assertions. Also include a checkstyle rules to ensure that the
the ExpectedException is not accidentally used in the future.

See gh-22894
This commit is contained in:
Phil Webb
2019-05-08 07:25:52 -07:00
committed by Sam Brannen
parent 7e6e3d7027
commit d7320de871
671 changed files with 3861 additions and 4601 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 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.
@@ -27,7 +27,7 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.tests.beans.CollectingReaderEventListener;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.MatcherAssert.*;
/**
* @author Torsten Juergeleit

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@@ -18,9 +18,7 @@ package org.springframework.transaction.event;
import java.lang.reflect.Method;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.event.ApplicationListenerMethodAdapter;
@@ -35,10 +33,6 @@ import static org.junit.Assert.*;
*/
public class ApplicationListenerMethodTransactionalAdapterTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void defaultPhase() {
Method m = ReflectionUtils.findMethod(SampleEvents.class, "defaultPhase", String.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 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.
@@ -27,9 +27,7 @@ import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
@@ -66,9 +64,6 @@ public class TransactionalEventListenerTests {
private TransactionTemplate transactionTemplate = new TransactionTemplate(new CallCountingTransactionManager());
@Rule
public final ExpectedException thrown = ExpectedException.none();
@After
public void closeContext() {

View File

@@ -240,7 +240,8 @@ public abstract class AbstractReactiveTransactionAspectTests {
else {
when(rtm.commit(status)).thenReturn(Mono.error(tex));
}
}else{
}
else {
when(rtm.commit(status)).thenReturn(Mono.empty());
when(rtm.rollback(status)).thenReturn(Mono.empty());
}
@@ -251,10 +252,10 @@ public abstract class AbstractReactiveTransactionAspectTests {
itb.exceptional(ex)
.as(StepVerifier::create)
.expectErrorSatisfies(actual -> {
if (rollbackException) {
assertThat(actual).isEqualTo(tex);
} else {
}
else {
assertThat(actual).isEqualTo(ex);
}
}).verify();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -43,7 +43,7 @@ import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.mockito.Mockito.*;
/**
* Test cases for AOP transaction management.

View File

@@ -58,4 +58,4 @@ public class ReactiveTransactionInterceptorTests extends AbstractReactiveTransac
return pf.getProxy();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -19,9 +19,7 @@ package org.springframework.transaction.interceptor;
import java.io.Serializable;
import java.util.Properties;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
@@ -33,6 +31,7 @@ import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.util.SerializationTestUtils;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
@@ -44,10 +43,6 @@ import static org.mockito.BDDMockito.*;
*/
public class TransactionInterceptorTests extends AbstractTransactionAspectTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Override
protected Object advised(Object target, PlatformTransactionManager ptm, TransactionAttributeSource[] tas) throws Exception {
TransactionInterceptor ti = new TransactionInterceptor();
@@ -154,9 +149,9 @@ public class TransactionInterceptorTests extends AbstractTransactionAspectTests
DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
attribute.setQualifier("fooTransactionManager");
thrown.expect(NoSuchBeanDefinitionException.class);
thrown.expectMessage("'fooTransactionManager'");
ti.determineTransactionManager(attribute);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() ->
ti.determineTransactionManager(attribute))
.withMessageContaining("'fooTransactionManager'");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -59,7 +59,8 @@ public class MockUOWManager implements UOWManager {
catch (Error | RuntimeException ex) {
this.status = UOW_STATUS_ROLLEDBACK;
throw ex;
} catch (Exception ex) {
}
catch (Exception ex) {
this.status = UOW_STATUS_ROLLEDBACK;
throw new UOWActionException(ex);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 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.
@@ -20,6 +20,9 @@ import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.UserTransaction;
import com.ibm.wsspi.uow.UOWAction;
import com.ibm.wsspi.uow.UOWException;
import com.ibm.wsspi.uow.UOWManager;
import org.junit.Test;
import org.springframework.dao.OptimisticLockingFailureException;
@@ -33,10 +36,6 @@ import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import com.ibm.wsspi.uow.UOWAction;
import com.ibm.wsspi.uow.UOWException;
import com.ibm.wsspi.uow.UOWManager;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;

View File

@@ -22,8 +22,8 @@ import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.transaction.IllegalTransactionStateException;
import org.springframework.transaction.ReactiveTransactionManager;
import org.springframework.transaction.ReactiveTransaction;
import org.springframework.transaction.ReactiveTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.DefaultTransactionDefinition;
@@ -42,9 +42,9 @@ public class ReactiveTransactionSupportTests {
tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS))
.subscriberContext(TransactionContextManager.createTransactionContext()).cast(GenericReactiveTransaction.class)
.as(StepVerifier::create).consumeNextWith(actual -> {
assertFalse(actual.hasTransaction());
}).verifyComplete();
.as(StepVerifier::create).consumeNextWith(actual ->
assertFalse(actual.hasTransaction())
).verifyComplete();
tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED))
.cast(GenericReactiveTransaction.class).subscriberContext(TransactionContextManager.createTransactionContext())