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

@@ -23,9 +23,9 @@ import org.junit.Test;
import org.springframework.aop.aspectj.AspectJAdviceParameterNameDiscoverer.AmbiguousBindingException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
/**
* Unit tests for the {@link AspectJAdviceParameterNameDiscoverer} class.
@@ -226,8 +226,7 @@ public class AspectJAdviceParameterNameDiscovererTests {
return candidate;
}
}
fail("Bad test specification, no method '" + name + "' found in test class");
return null;
throw new AssertionError("Bad test specification, no method '" + name + "' found in test class");
}
protected void assertParameterNames(Method method, String pointcut, String[] parameterNames) {
@@ -262,27 +261,20 @@ public class AspectJAdviceParameterNameDiscovererTests {
}
}
protected void assertException(Method method, String pointcut, Class<?> exceptionType, String message) {
protected void assertException(Method method, String pointcut, Class<? extends Throwable> exceptionType, String message) {
assertException(method, pointcut, null, null, exceptionType, message);
}
protected void assertException(
Method method, String pointcut, String returning, String throwing, Class<?> exceptionType, String message) {
protected void assertException(Method method, String pointcut, String returning,
String throwing, Class<? extends Throwable> exceptionType, String message) {
AspectJAdviceParameterNameDiscoverer discoverer = new AspectJAdviceParameterNameDiscoverer(pointcut);
discoverer.setRaiseExceptions(true);
discoverer.setReturningName(returning);
discoverer.setThrowingName(throwing);
try {
discoverer.getParameterNames(method);
fail("Expecting " + exceptionType.getName() + " with message '" + message + "'");
}
catch (RuntimeException expected) {
assertEquals("Expecting exception of type " + exceptionType.getName(),
exceptionType, expected.getClass());
assertEquals("Exception message does not match expected", message, expected.getMessage());
}
assertThatExceptionOfType(exceptionType).isThrownBy(() ->
discoverer.getParameterNames(method))
.withMessageContaining(message);
}

View File

@@ -36,10 +36,13 @@ import org.springframework.tests.sample.beans.ITestBean;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.tests.sample.beans.subpkg.DeepBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Rob Harrop
@@ -163,37 +166,25 @@ public class AspectJExpressionPointcutTests {
@Test
public void testFriendlyErrorOnNoLocationClassMatching() {
AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
try {
pc.matches(ITestBean.class);
fail();
}
catch (IllegalStateException ex) {
assertTrue(ex.getMessage().contains("expression"));
}
assertThatIllegalStateException().isThrownBy(() ->
pc.matches(ITestBean.class))
.withMessageContaining("expression");
}
@Test
public void testFriendlyErrorOnNoLocation2ArgMatching() {
AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
try {
pc.matches(getAge, ITestBean.class);
fail();
}
catch (IllegalStateException ex) {
assertTrue(ex.getMessage().contains("expression"));
}
assertThatIllegalStateException().isThrownBy(() ->
pc.matches(getAge, ITestBean.class))
.withMessageContaining("expression");
}
@Test
public void testFriendlyErrorOnNoLocation3ArgMatching() {
AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
try {
pc.matches(getAge, ITestBean.class, (Object[]) null);
fail();
}
catch (IllegalStateException ex) {
assertTrue(ex.getMessage().contains("expression"));
}
assertThatIllegalStateException().isThrownBy(() ->
pc.matches(getAge, ITestBean.class, (Object[]) null))
.withMessageContaining("expression");
}
@@ -248,14 +239,8 @@ public class AspectJExpressionPointcutTests {
@Test
public void testInvalidExpression() {
String expression = "execution(void org.springframework.tests.sample.beans.TestBean.setSomeNumber(Number) && args(Double)";
try {
getPointcut(expression).getClassFilter(); // call to getClassFilter forces resolution
fail("Invalid expression should throw IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
assertTrue(true);
}
assertThatIllegalArgumentException().isThrownBy(
getPointcut(expression)::getClassFilter); // call to getClassFilter forces resolution
}
private TestBean getAdvisedProxy(String pointcutExpression, CallCountingInterceptor interceptor) {
@@ -285,14 +270,9 @@ public class AspectJExpressionPointcutTests {
@Test
public void testWithUnsupportedPointcutPrimitive() {
String expression = "call(int org.springframework.tests.sample.beans.TestBean.getAge())";
try {
getPointcut(expression).getClassFilter(); // call to getClassFilter forces resolution...
fail("Should not support call pointcuts");
}
catch (UnsupportedPointcutPrimitiveException ex) {
assertEquals("Should not support call pointcut", PointcutPrimitive.CALL, ex.getUnsupportedPrimitive());
}
assertThatExceptionOfType(UnsupportedPointcutPrimitiveException.class).isThrownBy(() ->
getPointcut(expression).getClassFilter()) // call to getClassFilter forces resolution...
.satisfies(ex -> assertThat(ex.getUnsupportedPrimitive()).isEqualTo(PointcutPrimitive.CALL));
}
@Test

View File

@@ -37,12 +37,13 @@ import org.springframework.lang.Nullable;
import org.springframework.tests.sample.beans.ITestBean;
import org.springframework.tests.sample.beans.TestBean;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Rod Johnson
@@ -54,24 +55,14 @@ public class MethodInvocationProceedingJoinPointTests {
@Test
public void testingBindingWithJoinPoint() {
try {
AbstractAspectJAdvice.currentJoinPoint();
fail("Needs to be bound by interceptor action");
}
catch (IllegalStateException ex) {
// expected
}
assertThatIllegalStateException().isThrownBy(
AbstractAspectJAdvice::currentJoinPoint);
}
@Test
public void testingBindingWithProceedingJoinPoint() {
try {
AbstractAspectJAdvice.currentJoinPoint();
fail("Needs to be bound by interceptor action");
}
catch (IllegalStateException ex) {
// expected
}
assertThatIllegalStateException().isThrownBy(
AbstractAspectJAdvice::currentJoinPoint);
}
@Test
@@ -148,21 +139,8 @@ public class MethodInvocationProceedingJoinPointTests {
SourceLocation sloc = AbstractAspectJAdvice.currentJoinPoint().getSourceLocation();
assertEquals("Same source location must be returned on subsequent requests", sloc, AbstractAspectJAdvice.currentJoinPoint().getSourceLocation());
assertEquals(TestBean.class, sloc.getWithinType());
try {
sloc.getLine();
fail("Can't get line number");
}
catch (UnsupportedOperationException ex) {
// Expected
}
try {
sloc.getFileName();
fail("Can't get file name");
}
catch (UnsupportedOperationException ex) {
// Expected
}
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getLine);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getFileName);
}
});
ITestBean itb = (ITestBean) pf.getProxy();

View File

@@ -34,8 +34,8 @@ import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.core.OverridingClassLoader;
import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* @author Dave Syer
@@ -112,13 +112,8 @@ public class TrickyAspectJPointcutExpressionTests {
TestService bean = (TestService) factory.getProxy();
assertEquals(0, logAdvice.getCountThrows());
try {
bean.sayHello();
fail("Expected exception");
}
catch (TestException ex) {
assertEquals(message, ex.getMessage());
}
assertThatExceptionOfType(TestException.class).isThrownBy(
bean::sayHello).withMessageContaining(message);
assertEquals(1, logAdvice.getCountThrows());
}

View File

@@ -26,6 +26,8 @@ import org.springframework.tests.sample.beans.ITestBean;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.tests.sample.beans.subpkg.DeepBean;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -38,10 +40,11 @@ import static org.junit.Assert.assertTrue;
*/
public class TypePatternClassFilterTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void testInvalidPattern() {
// should throw - pattern must be recognized as invalid
new TypePatternClassFilter("-");
assertThatIllegalArgumentException().isThrownBy(() ->
new TypePatternClassFilter("-"));
}
@Test
@@ -79,14 +82,16 @@ public class TypePatternClassFilterTests {
assertTrue("matches Double",tpcf.matches(Double.class));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testSetTypePatternWithNullArgument() throws Exception {
new TypePatternClassFilter(null);
assertThatIllegalArgumentException().isThrownBy(() ->
new TypePatternClassFilter(null));
}
@Test(expected = IllegalStateException.class)
@Test
public void testInvocationOfMatchesMethodBlowsUpWhenNoTypePatternHasBeenSet() throws Exception {
new TypePatternClassFilter().matches(String.class);
assertThatIllegalStateException().isThrownBy(() ->
new TypePatternClassFilter().matches(String.class));
}
}

View File

@@ -58,14 +58,15 @@ import org.springframework.tests.sample.beans.ITestBean;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Abstract tests for AspectJAdvisorFactory.
@@ -86,26 +87,18 @@ public abstract class AbstractAspectJAdvisorFactoryTests {
@Test
public void testRejectsPerCflowAspect() {
try {
getFixture().getAdvisors(
new SingletonMetadataAwareAspectInstanceFactory(new PerCflowAspect(), "someBean"));
fail("Cannot accept cflow");
}
catch (AopConfigException ex) {
assertTrue(ex.getMessage().contains("PERCFLOW"));
}
assertThatExceptionOfType(AopConfigException.class).isThrownBy(() ->
getFixture().getAdvisors(
new SingletonMetadataAwareAspectInstanceFactory(new PerCflowAspect(), "someBean")))
.withMessageContaining("PERCFLOW");
}
@Test
public void testRejectsPerCflowBelowAspect() {
try {
getFixture().getAdvisors(
new SingletonMetadataAwareAspectInstanceFactory(new PerCflowBelowAspect(), "someBean"));
fail("Cannot accept cflowbelow");
}
catch (AopConfigException ex) {
assertTrue(ex.getMessage().contains("PERCFLOWBELOW"));
}
assertThatExceptionOfType(AopConfigException.class).isThrownBy(() ->
getFixture().getAdvisors(
new SingletonMetadataAwareAspectInstanceFactory(new PerCflowBelowAspect(), "someBean")))
.withMessageContaining("PERCFLOWBELOW");
}
@Test
@@ -365,12 +358,8 @@ public abstract class AbstractAspectJAdvisorFactoryTests {
assertFalse(lockable2.locked());
notLockable2.setIntValue(1);
lockable2.lock();
try {
notLockable2.setIntValue(32);
fail();
}
catch (IllegalStateException ex) {
}
assertThatIllegalStateException().isThrownBy(() ->
notLockable2.setIntValue(32));
assertTrue(lockable2.locked());
}
@@ -400,13 +389,8 @@ public abstract class AbstractAspectJAdvisorFactoryTests {
assertTrue("Already locked", lockable.locked());
lockable.lock();
assertTrue("Real target ignores locking", lockable.locked());
try {
lockable.unlock();
fail();
}
catch (UnsupportedOperationException ex) {
// Ok
}
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
lockable.unlock());
}
@Test
@@ -463,13 +447,8 @@ public abstract class AbstractAspectJAdvisorFactoryTests {
lockable.lock();
assertTrue(lockable.locked());
try {
itb.setName("Else");
fail("Should be locked");
}
catch (IllegalStateException ex) {
// Ok
}
assertThatIllegalStateException().as("Should be locked").isThrownBy(() ->
itb.setName("Else"));
lockable.unlock();
itb.setName("Tony");
}
@@ -482,14 +461,8 @@ public abstract class AbstractAspectJAdvisorFactoryTests {
new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException), "someBean"));
assertEquals("One advice method was found", 1, advisors.size());
ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class);
try {
itb.getAge();
fail();
}
catch (UnsupportedOperationException ex) {
assertSame(expectedException, ex);
}
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(
itb::getAge);
}
// TODO document this behaviour.
@@ -502,14 +475,8 @@ public abstract class AbstractAspectJAdvisorFactoryTests {
new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException), "someBean"));
assertEquals("One advice method was found", 1, advisors.size());
ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class);
try {
itb.getAge();
fail();
}
catch (UndeclaredThrowableException ex) {
assertSame(expectedException, ex.getCause());
}
assertThatExceptionOfType(UndeclaredThrowableException.class).isThrownBy(
itb::getAge).withCause(expectedException);
}
protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) {
@@ -555,17 +522,8 @@ public abstract class AbstractAspectJAdvisorFactoryTests {
assertEquals("", echo.echo(""));
assertEquals(1, afterReturningAspect.successCount);
assertEquals(0, afterReturningAspect.failureCount);
try {
echo.echo(new FileNotFoundException());
fail();
}
catch (FileNotFoundException ex) {
// Ok
}
catch (Exception ex) {
fail();
}
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() ->
echo.echo(new FileNotFoundException()));
assertEquals(1, afterReturningAspect.successCount);
assertEquals(1, afterReturningAspect.failureCount);
assertEquals(afterReturningAspect.failureCount + afterReturningAspect.successCount, afterReturningAspect.afterCount);
@@ -581,12 +539,14 @@ public abstract class AbstractAspectJAdvisorFactoryTests {
itb.getAge();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testDeclarePrecedenceNotSupported() {
TestBean target = new TestBean();
MetadataAwareAspectInstanceFactory aspectInstanceFactory = new SingletonMetadataAwareAspectInstanceFactory(
new DeclarePrecedenceShouldSucceed(), "someBean");
createProxy(target, getFixture().getAdvisors(aspectInstanceFactory), ITestBean.class);
assertThatIllegalArgumentException().isThrownBy(() -> {
MetadataAwareAspectInstanceFactory aspectInstanceFactory = new SingletonMetadataAwareAspectInstanceFactory(
new DeclarePrecedenceShouldSucceed(), "someBean");
createProxy(target, getFixture().getAdvisors(aspectInstanceFactory), ITestBean.class);
});
}

View File

@@ -30,6 +30,8 @@ import org.springframework.aop.aspectj.AspectJAdviceParameterNameDiscoverer;
import org.springframework.tests.sample.beans.ITestBean;
import org.springframework.tests.sample.beans.TestBean;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
/**
@@ -39,24 +41,26 @@ import static org.junit.Assert.assertEquals;
*/
public class ArgumentBindingTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void testBindingInPointcutUsedByAdvice() {
TestBean tb = new TestBean();
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(tb);
proxyFactory.addAspect(NamedPointcutWithArgs.class);
ITestBean proxiedTestBean = proxyFactory.getProxy();
proxiedTestBean.setName("Supercalifragalisticexpialidocious");
assertThatIllegalArgumentException().isThrownBy(() ->
proxiedTestBean.setName("Supercalifragalisticexpialidocious"));
}
@Test(expected = IllegalStateException.class)
@Test
public void testAnnotationArgumentNameBinding() {
TransactionalBean tb = new TransactionalBean();
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(tb);
proxyFactory.addAspect(PointcutWithAnnotationArgument.class);
ITransactionalBean proxiedTestBean = proxyFactory.getProxy();
proxiedTestBean.doInTransaction();
assertThatIllegalStateException().isThrownBy(
proxiedTestBean::doInTransaction);
}
@Test

View File

@@ -25,6 +25,7 @@ import org.springframework.aop.aspectj.AspectJExpressionPointcutTests;
import org.springframework.aop.framework.AopConfigException;
import org.springframework.tests.sample.beans.TestBean;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
@@ -75,14 +76,16 @@ public class AspectJPointcutAdvisorTests {
TestBean.class.getMethod("getSpouse"), TestBean.class));
}
@Test(expected = AopConfigException.class)
@Test
public void testPerCflowTarget() {
testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowAspect.class);
assertThatExceptionOfType(AopConfigException.class).isThrownBy(() ->
testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowAspect.class));
}
@Test(expected = AopConfigException.class)
@Test
public void testPerCflowBelowTarget() {
testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowBelowAspect.class);
assertThatExceptionOfType(AopConfigException.class).isThrownBy(() ->
testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowBelowAspect.class));
}
private void testIllegalInstantiationModel(Class<?> c) throws AopConfigException {

View File

@@ -23,6 +23,7 @@ import test.aop.PerTargetAspect;
import org.springframework.aop.Pointcut;
import org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactoryTests.ExceptionAspect;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
@@ -36,9 +37,10 @@ import static org.junit.Assert.assertTrue;
*/
public class AspectMetadataTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void testNotAnAspect() {
new AspectMetadata(String.class,"someBean");
assertThatIllegalArgumentException().isThrownBy(() ->
new AspectMetadata(String.class,"someBean"));
}
@Test

View File

@@ -28,6 +28,7 @@ import test.aop.PerThisAspect;
import org.springframework.util.SerializationTestUtils;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -38,10 +39,11 @@ import static org.junit.Assert.assertTrue;
*/
public class AspectProxyFactoryTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void testWithNonAspect() {
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean());
proxyFactory.addAspect(TestBean.class);
assertThatIllegalArgumentException().isThrownBy(() ->
proxyFactory.addAspect(TestBean.class));
}
@Test
@@ -74,10 +76,11 @@ public class AspectProxyFactoryTests {
assertEquals(2, proxy1.getAge());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testWithInstanceWithNonAspect() throws Exception {
AspectJProxyFactory pf = new AspectJProxyFactory();
pf.addAspect(new TestBean());
assertThatIllegalArgumentException().isThrownBy(() ->
pf.addAspect(new TestBean()));
}
@Test
@@ -111,10 +114,11 @@ public class AspectProxyFactoryTests {
assertEquals(target.getAge() * multiple, serializedProxy.getAge());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testWithNonSingletonAspectInstance() throws Exception {
AspectJProxyFactory pf = new AspectJProxyFactory();
pf.addAspect(new PerThisAspect());
assertThatIllegalArgumentException().isThrownBy(() ->
pf.addAspect(new PerThisAspect()));
}
@Test // SPR-13328

View File

@@ -23,8 +23,7 @@ import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.tests.TestResourceUtils.qualifiedResource;
/**
@@ -35,28 +34,20 @@ public class AopNamespaceHandlerPointcutErrorTests {
@Test
public void testDuplicatePointcutConfig() {
try {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
qualifiedResource(getClass(), "pointcutDuplication.xml"));
fail("parsing should have caused a BeanDefinitionStoreException");
}
catch (BeanDefinitionStoreException ex) {
assertTrue(ex.contains(BeanDefinitionParsingException.class));
}
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() ->
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
qualifiedResource(getClass(), "pointcutDuplication.xml")))
.satisfies(ex -> ex.contains(BeanDefinitionParsingException.class));
}
@Test
public void testMissingPointcutConfig() {
try {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
qualifiedResource(getClass(), "pointcutMissing.xml"));
fail("parsing should have caused a BeanDefinitionStoreException");
}
catch (BeanDefinitionStoreException ex) {
assertTrue(ex.contains(BeanDefinitionParsingException.class));
}
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() ->
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
qualifiedResource(getClass(), "pointcutMissing.xml")))
.satisfies(ex -> ex.contains(BeanDefinitionParsingException.class));
}
}

View File

@@ -26,6 +26,7 @@ import org.springframework.aop.SpringProxy;
import org.springframework.tests.sample.beans.ITestBean;
import org.springframework.tests.sample.beans.TestBean;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -125,11 +126,12 @@ public class AopProxyUtilsTests {
assertEquals(Comparable.class, userInterfaces[1]);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testProxiedUserInterfacesWithNoInterface() {
Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[0],
(proxy1, method, args) -> null);
AopProxyUtils.proxiedUserInterfaces(proxy);
assertThatIllegalArgumentException().isThrownBy(() ->
AopProxyUtils.proxiedUserInterfaces(proxy));
}
}

View File

@@ -42,6 +42,7 @@ import org.springframework.tests.sample.beans.IOther;
import org.springframework.tests.sample.beans.ITestBean;
import org.springframework.tests.sample.beans.TestBean;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
@@ -49,7 +50,6 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Also tests AdvisedSupport and ProxyCreatorSupport superclasses.
@@ -277,13 +277,9 @@ public class ProxyFactoryTests {
assertTrue(config.getAdvisors().length == oldCount);
try {
// Existing reference will fail
ts.getTimeStamp();
fail("Existing object won't implement this interface any more");
}
catch (RuntimeException ex) {
}
assertThatExceptionOfType(RuntimeException.class)
.as("Existing object won't implement this interface any more")
.isThrownBy(ts::getTimeStamp); // Existing reference will fail
assertFalse("Should no longer implement TimeStamped",
config.getProxy() instanceof TimeStamped);

View File

@@ -28,8 +28,9 @@ import org.junit.Test;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.tests.aop.advice.MethodCounter;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -39,10 +40,11 @@ import static org.mockito.Mockito.mock;
*/
public class ThrowsAdviceInterceptorTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void testNoHandlerMethods() {
// should require one handler method at least
new ThrowsAdviceInterceptor(new Object());
assertThatIllegalArgumentException().isThrownBy(() ->
new ThrowsAdviceInterceptor(new Object()));
}
@Test
@@ -64,13 +66,9 @@ public class ThrowsAdviceInterceptorTests {
Exception ex = new Exception();
MethodInvocation mi = mock(MethodInvocation.class);
given(mi.proceed()).willThrow(ex);
try {
ti.invoke(mi);
fail();
}
catch (Exception caught) {
assertEquals(ex, caught);
}
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
ti.invoke(mi))
.isSameAs(ex);
assertEquals(0, th.getCalls());
}
@@ -83,13 +81,9 @@ public class ThrowsAdviceInterceptorTests {
given(mi.getMethod()).willReturn(Object.class.getMethod("hashCode"));
given(mi.getThis()).willReturn(new Object());
given(mi.proceed()).willThrow(ex);
try {
ti.invoke(mi);
fail();
}
catch (Exception caught) {
assertEquals(ex, caught);
}
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() ->
ti.invoke(mi))
.isSameAs(ex);
assertEquals(1, th.getCalls());
assertEquals(1, th.getCalls("ioException"));
}
@@ -102,13 +96,9 @@ public class ThrowsAdviceInterceptorTests {
ConnectException ex = new ConnectException("");
MethodInvocation mi = mock(MethodInvocation.class);
given(mi.proceed()).willThrow(ex);
try {
ti.invoke(mi);
fail();
}
catch (Exception caught) {
assertEquals(ex, caught);
}
assertThatExceptionOfType(ConnectException.class).isThrownBy(() ->
ti.invoke(mi))
.isSameAs(ex);
assertEquals(1, th.getCalls());
assertEquals(1, th.getCalls("remoteException"));
}
@@ -131,13 +121,9 @@ public class ThrowsAdviceInterceptorTests {
ConnectException ex = new ConnectException("");
MethodInvocation mi = mock(MethodInvocation.class);
given(mi.proceed()).willThrow(ex);
try {
ti.invoke(mi);
fail();
}
catch (Throwable caught) {
assertEquals(t, caught);
}
assertThatExceptionOfType(Throwable.class).isThrownBy(() ->
ti.invoke(mi))
.isSameAs(t);
assertEquals(1, th.getCalls());
assertEquals(1, th.getCalls("remoteException"));
}

View File

@@ -20,7 +20,7 @@ import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.junit.Test;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
@@ -36,52 +36,60 @@ import static org.mockito.Mockito.verify;
*/
public class CustomizableTraceInterceptorTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void testSetEmptyEnterMessage() {
// Must not be able to set empty enter message
new CustomizableTraceInterceptor().setEnterMessage("");
assertThatIllegalArgumentException().isThrownBy(() ->
new CustomizableTraceInterceptor().setEnterMessage(""));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testSetEnterMessageWithReturnValuePlaceholder() {
// Must not be able to set enter message with return value placeholder
new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE);
assertThatIllegalArgumentException().isThrownBy(() ->
new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testSetEnterMessageWithExceptionPlaceholder() {
// Must not be able to set enter message with exception placeholder
new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION);
assertThatIllegalArgumentException().isThrownBy(() ->
new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testSetEnterMessageWithInvocationTimePlaceholder() {
// Must not be able to set enter message with invocation time placeholder
new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_INVOCATION_TIME);
assertThatIllegalArgumentException().isThrownBy(() ->
new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_INVOCATION_TIME));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testSetEmptyExitMessage() {
// Must not be able to set empty exit message
new CustomizableTraceInterceptor().setExitMessage("");
assertThatIllegalArgumentException().isThrownBy(() ->
new CustomizableTraceInterceptor().setExitMessage(""));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testSetExitMessageWithExceptionPlaceholder() {
// Must not be able to set exit message with exception placeholder
new CustomizableTraceInterceptor().setExitMessage(CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION);
assertThatIllegalArgumentException().isThrownBy(() ->
new CustomizableTraceInterceptor().setExitMessage(CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testSetEmptyExceptionMessage() {
// Must not be able to set empty exception message
new CustomizableTraceInterceptor().setExceptionMessage("");
assertThatIllegalArgumentException().isThrownBy(() ->
new CustomizableTraceInterceptor().setExceptionMessage(""));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testSetExceptionMethodWithReturnValuePlaceholder() {
// Must not be able to set exception message with return value placeholder
new CustomizableTraceInterceptor().setExceptionMessage(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE);
assertThatIllegalArgumentException().isThrownBy(() ->
new CustomizableTraceInterceptor().setExceptionMessage(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE));
}
@Test
@@ -114,12 +122,8 @@ public class CustomizableTraceInterceptorTests {
given(log.isTraceEnabled()).willReturn(true);
CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
try {
interceptor.invoke(methodInvocation);
fail("Must have propagated the IllegalArgumentException.");
}
catch (IllegalArgumentException expected) {
}
assertThatIllegalArgumentException().isThrownBy(() ->
interceptor.invoke(methodInvocation));
verify(log).trace(anyString());
verify(log).trace(anyString(), eq(exception));

View File

@@ -20,8 +20,8 @@ import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
@@ -64,12 +64,8 @@ public class DebugInterceptorTests {
given(log.isTraceEnabled()).willReturn(true);
DebugInterceptor interceptor = new StubDebugInterceptor(log);
try {
interceptor.invoke(methodInvocation);
fail("Must have propagated the IllegalArgumentException.");
}
catch (IllegalArgumentException expected) {
}
assertThatIllegalArgumentException().isThrownBy(() ->
interceptor.invoke(methodInvocation));
checkCallCountTotal(interceptor);
verify(log).trace(anyString());

View File

@@ -23,9 +23,9 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -69,12 +69,8 @@ public class JamonPerformanceMonitorInterceptorTests {
given(mi.getMethod()).willReturn(String.class.getMethod("toString"));
given(mi.proceed()).willThrow(new IllegalArgumentException());
try {
interceptor.invokeUnderTrace(mi, log);
fail("Must have propagated the IllegalArgumentException");
}
catch (IllegalArgumentException expected) {
}
assertThatIllegalArgumentException().isThrownBy(() ->
interceptor.invokeUnderTrace(mi, log));
assertEquals("Monitors must exist for the method invocation and 2 exceptions",
3, MonitorFactory.getNumRows());

View File

@@ -20,8 +20,8 @@ import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -70,12 +70,8 @@ public class PerformanceMonitorInterceptorTests {
Log log = mock(Log.class);
PerformanceMonitorInterceptor interceptor = new PerformanceMonitorInterceptor(true);
try {
interceptor.invokeUnderTrace(mi, log);
fail("Must have propagated the IllegalArgumentException.");
}
catch (IllegalArgumentException expected) {
}
assertThatIllegalArgumentException().isThrownBy(() ->
interceptor.invokeUnderTrace(mi, log));
verify(log).trace(anyString());
}

View File

@@ -20,7 +20,7 @@ import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.junit.Test;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
@@ -61,13 +61,8 @@ public class SimpleTraceInterceptorTests {
Log log = mock(Log.class);
final SimpleTraceInterceptor interceptor = new SimpleTraceInterceptor(true);
try {
interceptor.invokeUnderTrace(mi, log);
fail("Must have propagated the IllegalArgumentException.");
}
catch (IllegalArgumentException expected) {
}
assertThatIllegalArgumentException().isThrownBy(() ->
interceptor.invokeUnderTrace(mi, log));
verify(log).trace(anyString());
verify(log).trace(anyString(), eq(exception));

View File

@@ -20,6 +20,7 @@ import org.junit.Test;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.mock;
/**
@@ -33,24 +34,28 @@ public class DefaultScopedObjectTests {
private static final String GOOD_BEAN_NAME = "foo";
@Test(expected = IllegalArgumentException.class)
@Test
public void testCtorWithNullBeanFactory() throws Exception {
new DefaultScopedObject(null, GOOD_BEAN_NAME);
assertThatIllegalArgumentException().isThrownBy(() ->
new DefaultScopedObject(null, GOOD_BEAN_NAME));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testCtorWithNullTargetBeanName() throws Exception {
testBadTargetBeanName(null);
assertThatIllegalArgumentException().isThrownBy(() ->
testBadTargetBeanName(null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testCtorWithEmptyTargetBeanName() throws Exception {
testBadTargetBeanName("");
assertThatIllegalArgumentException().isThrownBy(() ->
testBadTargetBeanName(""));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testCtorWithJustWhitespacedTargetBeanName() throws Exception {
testBadTargetBeanName(" ");
assertThatIllegalArgumentException().isThrownBy(() ->
testBadTargetBeanName(" "));
}
private static void testBadTargetBeanName(final String badTargetBeanName) {

View File

@@ -34,6 +34,7 @@ import org.springframework.tests.sample.beans.SerializablePerson;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.util.SerializationTestUtils;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
@@ -49,10 +50,11 @@ import static org.mockito.Mockito.mock;
*/
public class DelegatingIntroductionInterceptorTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void testNullTarget() throws Exception {
// Shouldn't accept null target
new DelegatingIntroductionInterceptor(null);
assertThatIllegalArgumentException().isThrownBy(() ->
new DelegatingIntroductionInterceptor(null));
}
@Test

View File

@@ -31,9 +31,8 @@ import org.springframework.tests.sample.beans.SerializablePerson;
import org.springframework.tests.sample.beans.SideEffectBean;
import org.springframework.util.SerializationTestUtils;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.springframework.tests.TestResourceUtils.qualifiedResource;
/**
@@ -110,19 +109,11 @@ public class HotSwappableTargetSourceTests {
@Test
public void testRejectsSwapToNull() {
HotSwappableTargetSource swapper = (HotSwappableTargetSource) beanFactory.getBean("swapper");
IllegalArgumentException aopex = null;
try {
swapper.swap(null);
fail("Shouldn't be able to swap to invalid value");
}
catch (IllegalArgumentException ex) {
// Ok
aopex = ex;
}
assertThatIllegalArgumentException().as("Shouldn't be able to swap to invalid value").isThrownBy(() ->
swapper.swap(null))
.withMessageContaining("null");
// It shouldn't be corrupted, it should still work
testBasicFunctionality();
assertTrue(aopex.getMessage().contains("null"));
}
@Test

View File

@@ -26,7 +26,6 @@ import org.springframework.tests.sample.beans.SideEffectBean;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.springframework.tests.TestResourceUtils.qualifiedResource;
/**
@@ -154,12 +153,7 @@ public class ThreadLocalTargetSourceTests {
source.destroy();
// try second time
try {
source.getTarget();
}
catch (NullPointerException ex) {
fail("Should not throw NPE");
}
source.getTarget(); // Should not throw NPE
}
}