Refactor AssertJ assertions into more idiomatic ones

This commit refactors some AssertJ assertions into more idiomatic and
readable ones. Using the dedicated assertion instead of a generic one
will produce more meaningful error messages. 

For instance, consider collection size:
```
// expected: 5 but was: 2
assertThat(collection.size()).equals(5);
// Expected size: 5 but was: 2 in: [1, 2]
assertThat(collection).hasSize(5);
```

Closes gh-30104
This commit is contained in:
Krzysztof Krasoń
2023-04-04 17:34:07 +02:00
committed by GitHub
parent dd97ee4e99
commit 1734deca1e
371 changed files with 3177 additions and 3076 deletions

View File

@@ -210,7 +210,7 @@ public abstract class AbstractExpressionTests {
if (otherProperties != null && otherProperties.length != 0) {
// first one is expected position of the error within the string
int pos = (Integer) otherProperties[0];
assertThat(pos).as("reported position").isEqualTo(pos);
assertThat(ex.getPosition()).as("reported position").isEqualTo(pos);
if (otherProperties.length > 1) {
// Check inserts match
Object[] inserts = ex.getInserts();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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,29 +43,29 @@ public class ComparatorTests {
void testPrimitives() throws EvaluationException {
TypeComparator comparator = new StandardTypeComparator();
// primitive int
assertThat(comparator.compare(1, 2) < 0).isTrue();
assertThat(comparator.compare(1, 1) == 0).isTrue();
assertThat(comparator.compare(2, 1) > 0).isTrue();
assertThat(comparator.compare(1, 2)).isLessThan(0);
assertThat(comparator.compare(1, 1)).isEqualTo(0);
assertThat(comparator.compare(2, 1)).isGreaterThan(0);
assertThat(comparator.compare(1.0d, 2) < 0).isTrue();
assertThat(comparator.compare(1.0d, 1) == 0).isTrue();
assertThat(comparator.compare(2.0d, 1) > 0).isTrue();
assertThat(comparator.compare(1.0d, 2)).isLessThan(0);
assertThat(comparator.compare(1.0d, 1)).isEqualTo(0);
assertThat(comparator.compare(2.0d, 1)).isGreaterThan(0);
assertThat(comparator.compare(1.0f, 2) < 0).isTrue();
assertThat(comparator.compare(1.0f, 1) == 0).isTrue();
assertThat(comparator.compare(2.0f, 1) > 0).isTrue();
assertThat(comparator.compare(1.0f, 2)).isLessThan(0);
assertThat(comparator.compare(1.0f, 1)).isEqualTo(0);
assertThat(comparator.compare(2.0f, 1)).isGreaterThan(0);
assertThat(comparator.compare(1L, 2) < 0).isTrue();
assertThat(comparator.compare(1L, 1) == 0).isTrue();
assertThat(comparator.compare(2L, 1) > 0).isTrue();
assertThat(comparator.compare(1L, 2)).isLessThan(0);
assertThat(comparator.compare(1L, 1)).isEqualTo(0);
assertThat(comparator.compare(2L, 1)).isGreaterThan(0);
assertThat(comparator.compare(1, 2L) < 0).isTrue();
assertThat(comparator.compare(1, 1L) == 0).isTrue();
assertThat(comparator.compare(2, 1L) > 0).isTrue();
assertThat(comparator.compare(1, 2L)).isLessThan(0);
assertThat(comparator.compare(1, 1L)).isEqualTo(0);
assertThat(comparator.compare(2, 1L)).isGreaterThan(0);
assertThat(comparator.compare(1L, 2L) < 0).isTrue();
assertThat(comparator.compare(1L, 1L) == 0).isTrue();
assertThat(comparator.compare(2L, 1L) > 0).isTrue();
assertThat(comparator.compare(1L, 2L)).isLessThan(0);
assertThat(comparator.compare(1L, 1L)).isEqualTo(0);
assertThat(comparator.compare(2L, 1L)).isGreaterThan(0);
}
@Test
@@ -75,42 +75,42 @@ public class ComparatorTests {
BigDecimal bdOne = new BigDecimal("1");
BigDecimal bdTwo = new BigDecimal("2");
assertThat(comparator.compare(bdOne, bdTwo) < 0).isTrue();
assertThat(comparator.compare(bdOne, new BigDecimal("1")) == 0).isTrue();
assertThat(comparator.compare(bdTwo, bdOne) > 0).isTrue();
assertThat(comparator.compare(bdOne, bdTwo)).isLessThan(0);
assertThat(comparator.compare(bdOne, new BigDecimal("1"))).isEqualTo(0);
assertThat(comparator.compare(bdTwo, bdOne)).isGreaterThan(0);
assertThat(comparator.compare(1, bdTwo) < 0).isTrue();
assertThat(comparator.compare(1, bdOne) == 0).isTrue();
assertThat(comparator.compare(2, bdOne) > 0).isTrue();
assertThat(comparator.compare(1, bdTwo)).isLessThan(0);
assertThat(comparator.compare(1, bdOne)).isEqualTo(0);
assertThat(comparator.compare(2, bdOne)).isGreaterThan(0);
assertThat(comparator.compare(1.0d, bdTwo) < 0).isTrue();
assertThat(comparator.compare(1.0d, bdOne) == 0).isTrue();
assertThat(comparator.compare(2.0d, bdOne) > 0).isTrue();
assertThat(comparator.compare(1.0d, bdTwo)).isLessThan(0);
assertThat(comparator.compare(1.0d, bdOne)).isEqualTo(0);
assertThat(comparator.compare(2.0d, bdOne)).isGreaterThan(0);
assertThat(comparator.compare(1.0f, bdTwo) < 0).isTrue();
assertThat(comparator.compare(1.0f, bdOne) == 0).isTrue();
assertThat(comparator.compare(2.0f, bdOne) > 0).isTrue();
assertThat(comparator.compare(1.0f, bdTwo)).isLessThan(0);
assertThat(comparator.compare(1.0f, bdOne)).isEqualTo(0);
assertThat(comparator.compare(2.0f, bdOne)).isGreaterThan(0);
assertThat(comparator.compare(1L, bdTwo) < 0).isTrue();
assertThat(comparator.compare(1L, bdOne) == 0).isTrue();
assertThat(comparator.compare(2L, bdOne) > 0).isTrue();
assertThat(comparator.compare(1L, bdTwo)).isLessThan(0);
assertThat(comparator.compare(1L, bdOne)).isEqualTo(0);
assertThat(comparator.compare(2L, bdOne)).isGreaterThan(0);
}
@Test
void testNulls() throws EvaluationException {
TypeComparator comparator = new StandardTypeComparator();
assertThat(comparator.compare(null,"abc")<0).isTrue();
assertThat(comparator.compare(null,null)==0).isTrue();
assertThat(comparator.compare("abc",null)>0).isTrue();
assertThat(comparator.compare(null,"abc")).isLessThan(0);
assertThat(comparator.compare(null,null)).isEqualTo(0);
assertThat(comparator.compare("abc",null)).isGreaterThan(0);
}
@Test
void testObjects() throws EvaluationException {
TypeComparator comparator = new StandardTypeComparator();
assertThat(comparator.compare("a","a")==0).isTrue();
assertThat(comparator.compare("a","b")<0).isTrue();
assertThat(comparator.compare("b","a")>0).isTrue();
assertThat(comparator.compare("a","a")).isEqualTo(0);
assertThat(comparator.compare("a","b")).isLessThan(0);
assertThat(comparator.compare("b","a")).isGreaterThan(0);
}
@Test

View File

@@ -322,7 +322,7 @@ class EvaluationTests extends AbstractExpressionTests {
assertThat(value).isEqualTo("def");
e = parser.parseExpression("listOfStrings[2]");
value = e.getValue(ctx, String.class);
assertThat(value).isEqualTo("");
assertThat(value).isEmpty();
// Now turn off growing and reference off the end
StandardEvaluationContext failCtx = new StandardEvaluationContext(instance);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -196,7 +196,7 @@ class IndexingTests {
expression.setValue(this, "4");
}
catch (EvaluationException ex) {
assertThat(ex.getMessage().startsWith("EL1053E")).isTrue();
assertThat(ex.getMessage()).startsWith("EL1053E");
}
}
@@ -277,7 +277,7 @@ class IndexingTests {
assertThat(expression.getValue(this)).isEqualTo("bar");
}
catch (EvaluationException ex) {
assertThat(ex.getMessage().startsWith("EL1027E")).isTrue();
assertThat(ex.getMessage()).startsWith("EL1027E");
}
}
@@ -295,7 +295,7 @@ class IndexingTests {
assertThat(expression.getValue(this)).isEqualTo("bar");
}
catch (EvaluationException ex) {
assertThat(ex.getMessage().startsWith("EL1053E")).isTrue();
assertThat(ex.getMessage()).startsWith("EL1053E");
}
}
@@ -313,7 +313,7 @@ class IndexingTests {
assertThat(expression.getValue(this)).isEqualTo("bar");
}
catch (EvaluationException ex) {
assertThat(ex.getMessage().startsWith("EL1053E")).isTrue();
assertThat(ex.getMessage()).startsWith("EL1053E");
}
}
@@ -337,7 +337,7 @@ class IndexingTests {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("listOfScalarNotGeneric");
assertThat(expression.getValueTypeDescriptor(this).toString()).isEqualTo("java.util.ArrayList<?>");
assertThat(expression.getValue(this, String.class)).isEqualTo("");
assertThat(expression.getValue(this, String.class)).isEmpty();
}
@SuppressWarnings("unchecked")

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -72,7 +72,7 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
* them to be their boxed or unboxed variants) or compare object references. It does not
* compile expressions where numbers are of different types or when objects implement
* Comparable.
*
*
* Compiled nodes:
*
* TypeReference
@@ -1071,10 +1071,10 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
assertThat(expression.getValue(context).toString()).isEqualTo("a");
expression = parser.parseExpression("#append()");
assertThat(expression.getValue(context).toString()).isEqualTo("");
assertThat(expression.getValue(context).toString()).isEmpty();
assertThat(((SpelNodeImpl)((SpelExpression) expression).getAST()).isCompilable()).isTrue();
assertCanCompile(expression);
assertThat(expression.getValue(context).toString()).isEqualTo("");
assertThat(expression.getValue(context).toString()).isEmpty();
expression = parser.parseExpression("#append(#stringArray)");
assertThat(expression.getValue(context).toString()).isEqualTo("xyz");
@@ -1108,10 +1108,10 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
assertThat(expression.getValue(context).toString()).isEqualTo("ab");
expression = parser.parseExpression("#append2()");
assertThat(expression.getValue(context).toString()).isEqualTo("");
assertThat(expression.getValue(context).toString()).isEmpty();
assertThat(((SpelNodeImpl)((SpelExpression) expression).getAST()).isCompilable()).isTrue();
assertCanCompile(expression);
assertThat(expression.getValue(context).toString()).isEqualTo("");
assertThat(expression.getValue(context).toString()).isEmpty();
expression = parser.parseExpression("#append3(#stringArray)");
assertThat(expression.getValue(context, new SomeCompareMethod2()).toString()).isEqualTo("xyz");
@@ -3383,9 +3383,9 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
assertThat(expression.getValue(String.class)).isEqualTo("foo");
expression = parser.parseExpression(prefix + "2().output");
assertThat(expression.getValue(String.class)).isEqualTo("");
assertThat(expression.getValue(String.class)).isEmpty();
assertCanCompile(expression);
assertThat(expression.getValue(String.class)).isEqualTo("");
assertThat(expression.getValue(String.class)).isEmpty();
expression = parser.parseExpression(prefix + "3(1,2,3).output");
assertThat(expression.getValue(String.class)).isEqualTo("123");
@@ -3398,9 +3398,9 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
assertThat(expression.getValue(String.class)).isEqualTo("1");
expression = parser.parseExpression(prefix + "3().output");
assertThat(expression.getValue(String.class)).isEqualTo("");
assertThat(expression.getValue(String.class)).isEmpty();
assertCanCompile(expression);
assertThat(expression.getValue(String.class)).isEqualTo("");
assertThat(expression.getValue(String.class)).isEmpty();
expression = parser.parseExpression(prefix + "3('abc',5.0f,1,2,3).output");
assertThat(expression.getValue(String.class)).isEqualTo("abc:5.0:123");
@@ -5095,7 +5095,7 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
classloadersUsed.add(cEx.getClass().getClassLoader());
assertThat((int) expression.getValue(Integer.class)).isEqualTo(9);
}
assertThat(classloadersUsed.size() > 1).isTrue();
assertThat(classloadersUsed.size()).isGreaterThan(1);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -455,7 +455,7 @@ class SpelDocumentationTests extends AbstractExpressionTests {
void templating() throws Exception {
String randomPhrase =
parser.parseExpression("random number is ${T(java.lang.Math).random()}", new TemplatedParserContext()).getValue(String.class);
assertThat(randomPhrase.startsWith("random number")).isTrue();
assertThat(randomPhrase).startsWith("random number");
}
static class TemplatedParserContext implements ParserContext {

View File

@@ -441,8 +441,8 @@ class SpelReproTests extends AbstractExpressionTests {
catch (SpelEvaluationException see) {
assertThat(see.getMessageCode()).isEqualTo(SpelMessage.EXCEPTION_DURING_BEAN_RESOLUTION);
assertThat(see.getInserts()[0]).isEqualTo("goo");
assertThat(see.getCause() instanceof AccessException).isTrue();
assertThat(see.getCause().getMessage().startsWith("DONT")).isTrue();
assertThat(see.getCause()).isInstanceOf(AccessException.class);
assertThat(see.getCause().getMessage()).startsWith("DONT");
}
// bean exists
@@ -807,7 +807,7 @@ class SpelReproTests extends AbstractExpressionTests {
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expression = parser.parseRaw("T(org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver).CONST");
Object value = expression.getValue(context);
assertThat(Reserver.CONST).isEqualTo(value);
assertThat(value).isEqualTo(Reserver.CONST);
}
/**
@@ -1373,7 +1373,7 @@ class SpelReproTests extends AbstractExpressionTests {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("new java.util.ArrayList(#root)");
Object value = expr.getValue(coll);
assertThat(value instanceof ArrayList).isTrue();
assertThat(value).isInstanceOf(ArrayList.class);
@SuppressWarnings("rawtypes")
ArrayList list = (ArrayList) value;
assertThat(list.get(0)).isEqualTo("one");
@@ -1448,7 +1448,7 @@ class SpelReproTests extends AbstractExpressionTests {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("T(java.util.Arrays).asList('')");
Object value = expression.getValue();
assertThat(value instanceof List).isTrue();
assertThat(value).isInstanceOf(List.class);
assertThat(((List) value).isEmpty()).isTrue();
}
@@ -1458,7 +1458,7 @@ class SpelReproTests extends AbstractExpressionTests {
sec.setVariable("iterable", Collections.emptyList());
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("T(org.springframework.expression.spel.SpelReproTests.FooLists).newArrayList(#iterable)");
assertThat(expression.getValue(sec) instanceof ArrayList).isTrue();
assertThat(expression.getValue(sec)).isInstanceOf(ArrayList.class);
}
@Test
@@ -1467,13 +1467,13 @@ class SpelReproTests extends AbstractExpressionTests {
Expression expression = parser.parseExpression("T(org.springframework.expression.spel.SpelReproTests.DistanceEnforcer).from(#no)");
StandardEvaluationContext sec = new StandardEvaluationContext();
sec.setVariable("no", 1);
assertThat(expression.getValue(sec).toString().startsWith("Integer")).isTrue();
assertThat(expression.getValue(sec).toString()).startsWith("Integer");
sec = new StandardEvaluationContext();
sec.setVariable("no", 1.0F);
assertThat(expression.getValue(sec).toString().startsWith("Number")).isTrue();
assertThat(expression.getValue(sec).toString()).startsWith("Number");
sec = new StandardEvaluationContext();
sec.setVariable("no", "1.0");
assertThat(expression.getValue(sec).toString().startsWith("Object")).isTrue();
assertThat(expression.getValue(sec).toString()).startsWith("Object");
}
@Test

View File

@@ -75,7 +75,7 @@ class TemplateExpressionParsingTests extends AbstractExpressionTests {
expr = parser.parseExpression("", DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT);
o = expr.getValue();
assertThat(o.toString()).isEqualTo("");
assertThat(o.toString()).isEmpty();
expr = parser.parseExpression("abc", DOLLAR_SIGN_TEMPLATE_PARSER_CONTEXT);
o = expr.getValue();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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.
@@ -193,37 +193,37 @@ class SpelParserTests {
@Test
void booleanOperators() {
SpelExpression expr = new SpelExpressionParser().parseRaw("true");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.TRUE);
assertThat(expr.getValue(Boolean.class)).isTrue();
expr = new SpelExpressionParser().parseRaw("false");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.FALSE);
assertThat(expr.getValue(Boolean.class)).isFalse();
expr = new SpelExpressionParser().parseRaw("false and false");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.FALSE);
assertThat(expr.getValue(Boolean.class)).isFalse();
expr = new SpelExpressionParser().parseRaw("true and (true or false)");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.TRUE);
assertThat(expr.getValue(Boolean.class)).isTrue();
expr = new SpelExpressionParser().parseRaw("true and true or false");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.TRUE);
assertThat(expr.getValue(Boolean.class)).isTrue();
expr = new SpelExpressionParser().parseRaw("!true");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.FALSE);
assertThat(expr.getValue(Boolean.class)).isFalse();
expr = new SpelExpressionParser().parseRaw("!(false or true)");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.FALSE);
assertThat(expr.getValue(Boolean.class)).isFalse();
}
@Test
void booleanOperators_symbolic_spr9614() {
SpelExpression expr = new SpelExpressionParser().parseRaw("true");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.TRUE);
assertThat(expr.getValue(Boolean.class)).isTrue();
expr = new SpelExpressionParser().parseRaw("false");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.FALSE);
assertThat(expr.getValue(Boolean.class)).isFalse();
expr = new SpelExpressionParser().parseRaw("false && false");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.FALSE);
assertThat(expr.getValue(Boolean.class)).isFalse();
expr = new SpelExpressionParser().parseRaw("true && (true || false)");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.TRUE);
assertThat(expr.getValue(Boolean.class)).isTrue();
expr = new SpelExpressionParser().parseRaw("true && true || false");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.TRUE);
assertThat(expr.getValue(Boolean.class)).isTrue();
expr = new SpelExpressionParser().parseRaw("!true");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.FALSE);
assertThat(expr.getValue(Boolean.class)).isFalse();
expr = new SpelExpressionParser().parseRaw("!(false || true)");
assertThat(expr.getValue(Boolean.class)).isEqualTo(Boolean.FALSE);
assertThat(expr.getValue(Boolean.class)).isFalse();
}
@Test