Use new AssertJ exception assertions

This commit is contained in:
Sam Brannen
2022-05-31 14:08:28 +02:00
parent 9d324e59a0
commit 1beb7068f6
45 changed files with 554 additions and 803 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@@ -33,7 +33,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.testresources.PlaceOfBirth;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* Tests invocation of constructors.
@@ -127,8 +127,8 @@ public class ConstructorInvocationTests extends AbstractExpressionTests {
// 4 will make it throw a checked exception - this will be wrapped by spel on the
// way out
eContext.setVariable("bar", 4);
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
expr.getValue(eContext))
assertThatException()
.isThrownBy(() -> expr.getValue(eContext))
.withMessageContaining("Tester");
// A problem occurred whilst attempting to construct an object of type
// 'org.springframework.expression.spel.ConstructorInvocationTests$Tester'
@@ -139,7 +139,7 @@ public class ConstructorInvocationTests extends AbstractExpressionTests {
// 1 will make it throw a RuntimeException - SpEL will let this through
eContext.setVariable("bar", 1);
assertThatExceptionOfType(Exception.class)
assertThatException()
.isThrownBy(() -> expr.getValue(eContext))
.isNotInstanceOf(SpelEvaluationException.class);
// A problem occurred whilst attempting to construct an object of type

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@@ -39,6 +39,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.testresources.PlaceOfBirth;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
@@ -121,7 +122,8 @@ public class MethodInvocationTests extends AbstractExpressionTests {
// Now cause it to throw an exception:
eContext.setVariable("bar", 1);
assertThatExceptionOfType(Exception.class).isThrownBy(() -> expr.getValue(eContext))
assertThatException()
.isThrownBy(() -> expr.getValue(eContext))
.isNotInstanceOf(SpelEvaluationException.class);
// If counter is 4 then the method got called twice!
@@ -149,7 +151,7 @@ public class MethodInvocationTests extends AbstractExpressionTests {
Expression expr = parser.parseExpression("throwException(#bar)");
context.setVariable("bar", 2);
assertThatExceptionOfType(Exception.class)
assertThatException()
.isThrownBy(() -> expr.getValue(context))
.isNotInstanceOf(SpelEvaluationException.class);
}
@@ -166,7 +168,8 @@ public class MethodInvocationTests extends AbstractExpressionTests {
Expression expr = parser.parseExpression("throwException(#bar)");
context.setVariable("bar", 4);
assertThatExceptionOfType(ExpressionInvocationTargetException.class).isThrownBy(() -> expr.getValue(context))
assertThatExceptionOfType(ExpressionInvocationTargetException.class)
.isThrownBy(() -> expr.getValue(context))
.satisfies(ex -> assertThat(ex.getCause().getClass().getName()).isEqualTo(
"org.springframework.expression.spel.testresources.Inventor$TestException"));
}

View File

@@ -46,6 +46,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.testdata.PersonInOtherPackage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.within;
import static org.assertj.core.api.InstanceOfAssertFactories.BOOLEAN;
@@ -1252,8 +1253,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
ctx.setVariable("target", "123");
assertThat(expression.getValue(ctx)).isEqualTo("123");
ctx.setVariable("target", 42);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
expression.getValue(ctx))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> expression.getValue(ctx))
.withCauseInstanceOf(ClassCastException.class);
ctx.setVariable("target", "abc");
@@ -1264,8 +1265,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
ctx.setVariable("target", "1");
assertThat(expression.getValue(ctx)).isEqualTo('1');
ctx.setVariable("target", 42);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
expression.getValue(ctx))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> expression.getValue(ctx))
.withCauseInstanceOf(ClassCastException.class);
}
@@ -3998,8 +3999,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
assertThat(expression.getValue(is)).isEqualTo(2);
assertCanCompile(expression);
assertThat(expression.getValue(is)).isEqualTo(2);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
expression.getValue(strings))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> expression.getValue(strings))
.withCauseInstanceOf(ClassCastException.class);
SpelCompiler.revertToInterpreted(expression);
assertThat(expression.getValue(strings)).isEqualTo("b");
@@ -4026,8 +4027,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
assertCanCompile(expression);
tc.reset();
tc.obj=42;
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
expression.getValue(tc))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> expression.getValue(tc))
.withCauseInstanceOf(ClassCastException.class);
@@ -4035,8 +4036,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
expression = parser.parseExpression("#root.charAt(0)");
assertThat(expression.getValue("abc")).isEqualTo('a');
assertCanCompile(expression);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
expression.getValue(42))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> expression.getValue(42))
.withCauseInstanceOf(ClassCastException.class);
}
@@ -5148,7 +5149,7 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
}
private void assertGetValueFail(Expression expression) {
assertThatExceptionOfType(Exception.class).isThrownBy(expression::getValue);
assertThatException().isThrownBy(expression::getValue);
}
public static void assertIsCompiled(Expression expression) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@@ -62,6 +62,7 @@ import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@@ -104,8 +105,7 @@ class SpelReproTests extends AbstractExpressionTests {
expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(null)");
assertThat(expr.getValue(context)).isNull();
expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull2(null)");
assertThatExceptionOfType(EvaluationException.class).isThrownBy(
expr::getValue);
assertThatExceptionOfType(EvaluationException.class).isThrownBy(expr::getValue);
context.setTypeLocator(new MyTypeLocator());
// varargs
@@ -393,15 +393,15 @@ class SpelReproTests extends AbstractExpressionTests {
private void checkTemplateParsingError(String expression, ParserContext context, String expectedMessage) {
SpelExpressionParser parser = new SpelExpressionParser();
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
parser.parseExpression(expression, context))
assertThatException()
.isThrownBy(() -> parser.parseExpression(expression, context))
.satisfies(ex -> {
String message = ex.getMessage();
if (ex instanceof ExpressionException) {
message = ((ExpressionException) ex).getSimpleMessage();
}
assertThat(message).isEqualTo(expectedMessage);
});
String message = ex.getMessage();
if (ex instanceof ExpressionException) {
message = ((ExpressionException) ex).getSimpleMessage();
}
assertThat(message).isEqualTo(expectedMessage);
});
}
@@ -485,15 +485,15 @@ class SpelReproTests extends AbstractExpressionTests {
assertThat(expr.getValue()).isNull();
// Different parts of ternary expression are null
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
new SpelExpressionParser().parseRaw("(?'abc':'default')").getValue(context))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> new SpelExpressionParser().parseRaw("(?'abc':'default')").getValue(context))
.satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.TYPE_CONVERSION_ERROR));
expr = new SpelExpressionParser().parseRaw("(false?'abc':null)");
assertThat(expr.getValue()).isNull();
// Assignment
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
new SpelExpressionParser().parseRaw("(='default')").getValue(context))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> new SpelExpressionParser().parseRaw("(='default')").getValue(context))
.satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.SETVALUE_NOT_SUPPORTED));
}
@@ -1232,8 +1232,8 @@ class SpelReproTests extends AbstractExpressionTests {
void SPR16123() {
ExpressionParser parser = new SpelExpressionParser();
parser.parseExpression("simpleProperty").setValue(new BooleanHolder(), null);
assertThatExceptionOfType(EvaluationException.class).isThrownBy(() ->
parser.parseExpression("primitiveProperty").setValue(new BooleanHolder(), null));
assertThatExceptionOfType(EvaluationException.class)
.isThrownBy(() -> parser.parseExpression("primitiveProperty").setValue(new BooleanHolder(), null));
}
@Test
@@ -1249,8 +1249,8 @@ class SpelReproTests extends AbstractExpressionTests {
}
private void doTestSpr10146(String expression, String expectedMessage) {
assertThatExceptionOfType(SpelParseException.class).isThrownBy(() ->
new SpelExpressionParser().parseExpression(expression))
assertThatExceptionOfType(SpelParseException.class)
.isThrownBy(() -> new SpelExpressionParser().parseExpression(expression))
.withMessageContaining(expectedMessage);
}
@@ -1275,8 +1275,8 @@ class SpelReproTests extends AbstractExpressionTests {
@Test
void SPR10328() {
assertThatExceptionOfType(SpelParseException.class).isThrownBy(() ->
parser.parseExpression("$[]"))
assertThatExceptionOfType(SpelParseException.class)
.isThrownBy(() -> parser.parseExpression("$[]"))
.withMessageContaining("EL1071E: A required selection expression has not been specified");
}
@@ -1363,8 +1363,8 @@ class SpelReproTests extends AbstractExpressionTests {
StandardEvaluationContext context = new StandardEvaluationContext();
Spr11142 rootObject = new Spr11142();
Expression expression = parser.parseExpression("something");
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
expression.getValue(context, rootObject))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> expression.getValue(context, rootObject))
.withMessageContaining("'something' cannot be found");
}
@@ -1535,19 +1535,19 @@ class SpelReproTests extends AbstractExpressionTests {
expr = new SpelExpressionParser().parseRaw("&foo");
assertThat(expr.getValue(context)).isEqualTo("foo factory");
assertThatExceptionOfType(SpelParseException.class).isThrownBy(() ->
new SpelExpressionParser().parseRaw("&@foo"))
assertThatExceptionOfType(SpelParseException.class)
.isThrownBy(() -> new SpelExpressionParser().parseRaw("&@foo"))
.satisfies(ex -> {
assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.INVALID_BEAN_REFERENCE);
assertThat(ex.getPosition()).isEqualTo(0);
});
assertThatExceptionOfType(SpelParseException.class).isThrownBy(() ->
new SpelExpressionParser().parseRaw("@&foo"))
.satisfies(ex -> {
assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.INVALID_BEAN_REFERENCE);
assertThat(ex.getPosition()).isEqualTo(0);
});
assertThatExceptionOfType(SpelParseException.class)
.isThrownBy(() -> new SpelExpressionParser().parseRaw("@&foo"))
.satisfies(ex -> {
assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.INVALID_BEAN_REFERENCE);
assertThat(ex.getPosition()).isEqualTo(0);
});
}
@Test