From 6004455b1c1db171cf1996a50ae5f8c3343663c7 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 19 Dec 2024 22:41:29 +0100 Subject: [PATCH] Apply AssertJ best practices --- .../RecordObjectAccessorCopyingTest.java | 3 +-- .../MultipleTypeEqualsVerifierTest.java | 6 ++--- .../operational/OriginalStateTest.java | 2 +- .../integration/operational/ReportTest.java | 2 +- ...qualsVerifierInternalBugExceptionTest.java | 2 +- .../exceptions/NoValueExceptionTest.java | 2 +- .../exceptions/RecursionExceptionTest.java | 2 +- .../instantiation/SubjectCreatorTest.java | 3 +-- .../vintage/VintageValueProviderTest.java | 2 +- .../InPlaceObjectAccessorScramblingTest.java | 8 +++---- .../ConditionalInstantiatorTest.java | 24 +++++++++---------- .../reflection/FieldIterableTest.java | 10 ++++---- .../AnnotationCacheBuilderTest.java | 4 ++-- .../internal/util/ValidationsTest.java | 2 +- 14 files changed, 35 insertions(+), 37 deletions(-) diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessorCopyingTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessorCopyingTest.java index 3c9d68cdf..9c44be00d 100644 --- a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessorCopyingTest.java +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessorCopyingTest.java @@ -18,8 +18,7 @@ void copyHappyPath() { Object original = instantiate(SimpleRecord.class); Object copy = copyOf(original); - assertThat(copy).isNotSameAs(original); - assertThat(copy).isEqualTo(original); + assertThat(copy).isNotSameAs(original).isEqualTo(original); } @Test diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/MultipleTypeEqualsVerifierTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/MultipleTypeEqualsVerifierTest.java index c0ddf8472..1110b1ec2 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/MultipleTypeEqualsVerifierTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/MultipleTypeEqualsVerifierTest.java @@ -241,7 +241,7 @@ void succeed_whenCallingForPackageRecursivelyOnAPackageContainingFailingClasses_ void succeed_whenReportingOnSeveralCorrectClasses() { List reports = EqualsVerifier.forClasses(A.class, B.class, C.class).report(); - assertThat(reports.size()).isEqualTo(3); + assertThat(reports).hasSize(3); assertSuccessful(reports.get(0), A.class); assertSuccessful(reports.get(1), B.class); assertSuccessful(reports.get(2), C.class); @@ -251,7 +251,7 @@ void succeed_whenReportingOnSeveralCorrectClasses() { void fail_whenReportingOnOneIncorrectClass() { List reports = EqualsVerifier.forClasses(A.class, IncorrectM.class, C.class).report(); - assertThat(reports.size()).isEqualTo(3); + assertThat(reports).hasSize(3); assertSuccessful(reports.get(0), A.class); assertSuccessful(reports.get(2), C.class); assertUnsuccessful(reports.get(1), IncorrectM.class, "Subclass: equals is not final."); @@ -262,7 +262,7 @@ void fail_whenReportingOnTwoIncorrectClasses() { List reports = EqualsVerifier.forClasses(A.class, IncorrectM.class, C.class, IncorrectN.class).report(); - assertThat(reports.size()).isEqualTo(4); + assertThat(reports).hasSize(4); assertSuccessful(reports.get(0), A.class); assertSuccessful(reports.get(2), C.class); assertUnsuccessful(reports.get(1), IncorrectM.class, "Subclass: equals is not final."); diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/OriginalStateTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/OriginalStateTest.java index 0e885ff54..8e5c72c80 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/OriginalStateTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/OriginalStateTest.java @@ -53,7 +53,7 @@ void allValuesReturnToOriginalState_whenEqualsVerifierIsFinishedWithException() } catch (AssertionError e) { // Make sure EV fails on a check that actually mutates fields. - assertThat(e.getMessage().contains("Mutability")).isTrue(); + assertThat(e.getMessage()).contains("Mutability"); } catch (Throwable ignored) { fail("EqualsVerifier should have failed on FailingEqualsContainerContainer with a different exception."); diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/ReportTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/ReportTest.java index af4fbb4f7..5ed625627 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/ReportTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/ReportTest.java @@ -18,7 +18,7 @@ void isEmptyWhenClassIsCorrect() { assertThat(report.getType()).isEqualTo(FinalPoint.class); assertThat(report.isSuccessful()).isTrue(); - assertThat(report.getMessage()).isEqualTo(""); + assertThat(report.getMessage()).isEmpty(); assertThat(report.getCause()).isNull(); } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/EqualsVerifierInternalBugExceptionTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/EqualsVerifierInternalBugExceptionTest.java index 2b2bde395..89649197b 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/EqualsVerifierInternalBugExceptionTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/EqualsVerifierInternalBugExceptionTest.java @@ -60,7 +60,7 @@ private void assertMessagePreamble() { } private void assertNoCause() { - assertThat(actual.getCause()).isEqualTo(null); + assertThat(actual.getCause()).isNull(); } private void assertCause(Throwable cause) { diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/NoValueExceptionTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/NoValueExceptionTest.java index 376330b01..205c93df8 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/NoValueExceptionTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/NoValueExceptionTest.java @@ -11,6 +11,6 @@ class NoValueExceptionTest { void description() { TypeTag tag = new TypeTag(String.class); NoValueException e = new NoValueException(tag); - assertThat(e.getDescription().contains("String")).isTrue(); + assertThat(e.getDescription()).contains("String"); } } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/RecursionExceptionTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/RecursionExceptionTest.java index 32aae0c83..c231232b4 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/RecursionExceptionTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/RecursionExceptionTest.java @@ -20,7 +20,7 @@ void descriptionContainsAllTypes() { String message = new RecursionException(stack).getDescription(); for (TypeTag tag : stack) { - assertThat(message.contains(tag.toString())).isTrue(); + assertThat(message).contains(tag.toString()); } } } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/SubjectCreatorTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/SubjectCreatorTest.java index e9e838d7a..9661a30f6 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/SubjectCreatorTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/SubjectCreatorTest.java @@ -163,8 +163,7 @@ void copy() { SomeClass original = new SomeClass(I_RED, I_RED, S_RED); actual = sut.copy(original); - assertThat(actual).isEqualTo(expected); - assertThat(actual).isNotSameAs(expected); + assertThat(actual).isEqualTo(expected).isNotSameAs(expected); } @Test diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/VintageValueProviderTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/VintageValueProviderTest.java index e32d26286..ba5fac828 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/VintageValueProviderTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/VintageValueProviderTest.java @@ -46,7 +46,7 @@ void sanityTestFactoryIncreasesStringLength() { @Test void provide() { Optional> actual = vp.provide(POINT_TAG); - assertThat(actual.get()).isEqualTo(Tuple.of(new Point(42, 42), new Point(1337, 1337), new Point(42, 42))); + assertThat(actual).contains(Tuple.of(new Point(42, 42), new Point(1337, 1337), new Point(42, 42))); } @Test diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessorScramblingTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessorScramblingTest.java index eaa0b1438..81a2300e3 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessorScramblingTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessorScramblingTest.java @@ -114,14 +114,14 @@ void scramblePrivateFinalPoint() { void scrambleNestedGenerics() { GenericContainerContainer foo = new GenericContainerContainer(); - assertThat(foo.strings.ts.isEmpty()).isTrue(); - assertThat(foo.points.ts.isEmpty()).isTrue(); + assertThat(foo.strings.ts).isEmpty(); + assertThat(foo.points.ts).isEmpty(); doScramble(foo); - assertThat(foo.strings.ts.isEmpty()).isFalse(); + assertThat(foo.strings.ts).isNotEmpty(); assertThat(foo.strings.ts.get(0).getClass()).isEqualTo(String.class); - assertThat(foo.points.ts.isEmpty()).isFalse(); + assertThat(foo.points.ts).isNotEmpty(); assertThat(foo.points.ts.get(0).getClass()).isEqualTo(Point.class); } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/ConditionalInstantiatorTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/ConditionalInstantiatorTest.java index 0d6916296..a90937d03 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/ConditionalInstantiatorTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/ConditionalInstantiatorTest.java @@ -33,7 +33,7 @@ void resolveReturnsNull_whenTypeDoesntExist() { ci = new ConditionalInstantiator(THIS_TYPE_DOES_NOT_EXIST); Class actual = ci.resolve(); - assertThat(actual).isEqualTo(null); + assertThat(actual).isNull(); } @Test @@ -49,7 +49,7 @@ void objectIsInstantiatedCorrectly_whenValidConstructorParametersAreProvided() { void nullIsReturned_whenInstantiateIsCalled_givenTypeDoesNotExist() { ci = new ConditionalInstantiator(THIS_TYPE_DOES_NOT_EXIST); Object actual = ci.instantiate(classes(String.class), objects("nope")); - assertThat(actual).isEqualTo(null); + assertThat(actual).isNull(); } @Test @@ -65,7 +65,7 @@ void nullIsReturned_whenInvalidConstructorParametersAreProvided_givenFalse() { ci = new ConditionalInstantiator("java.util.GregorianCalendar", false); Object actual = ci.instantiate(classes(int.class, int.class, int.class), objects(1999, 31, "hello")); - assertThat(actual).isEqualTo(null); + assertThat(actual).isNull(); } @Test @@ -81,7 +81,7 @@ void objectIsInstantiatedCorrectly_whenValidFactoryMethodAndParametersAreProvide void nullIsReturned_whenFactoryIsCalled_givenTypeDoesNotExist() { ci = new ConditionalInstantiator(THIS_TYPE_DOES_NOT_EXIST); Object actual = ci.callFactory("factory", classes(String.class), objects("nope")); - assertThat(actual).isEqualTo(null); + assertThat(actual).isNull(); } @Test @@ -97,7 +97,7 @@ void nullIsReturned_whenInvalidMethodNameIsProvided_givenFalse() { ci = new ConditionalInstantiator("java.lang.Integer", false); Object actual = ci.callFactory("thisMethodDoesntExist", classes(int.class), objects(42)); - assertThat(actual).isEqualTo(null); + assertThat(actual).isNull(); } @Test @@ -113,7 +113,7 @@ void nullIsReturned_whenInvalidFactoryMethodParametersAreProvided_givenFalse() { ci = new ConditionalInstantiator("java.lang.Integer", false); Object actual = ci.callFactory("valueOf", classes(int.class, int.class), objects(42)); - assertThat(actual).isEqualTo(null); + assertThat(actual).isNull(); } @Test @@ -129,7 +129,7 @@ void objectIsInstantiatedCorrectly_whenValidExternalFactoryMethodAndParametersAr void nullIsReturned_whenExternalFactoryIsCalled_givenTypeDoesNotExist() { ci = new ConditionalInstantiator(THIS_TYPE_DOES_NOT_EXIST); Object actual = ci.callFactory("java.util.Collections", "emptyList", classes(), objects()); - assertThat(actual).isEqualTo(null); + assertThat(actual).isNull(); } @Test @@ -145,7 +145,7 @@ void nullIsReturned_whenExternalFactoryIsCalled_givenFactoryTypeDoesNotExist_giv ci = new ConditionalInstantiator("java.util.List", false); Object actual = ci.callFactory("java.util.ThisTypeDoesNotExist", "emptyList", classes(), objects()); - assertThat(actual).isEqualTo(null); + assertThat(actual).isNull(); } @Test @@ -162,7 +162,7 @@ void nullIsReturned_whenInvalidExternalFactoryMethodNameIsProvided_givenFalse() ci = new ConditionalInstantiator("java.util.List", false); Object actual = ci.callFactory("java.util.Collections", "thisMethodDoesntExist", classes(), objects()); - assertThat(actual).isEqualTo(null); + assertThat(actual).isNull(); } @Test @@ -179,14 +179,14 @@ void nullIsReturned_whenInvalidExternalFactoryMethodParametersAreProvided_givenF ci = new ConditionalInstantiator("java.util.List", false); Object actual = ci.callFactory("java.util.Collections", "emptyList", classes(int.class), objects(42)); - assertThat(actual).isEqualTo(null); + assertThat(actual).isNull(); } @Test void nullIsReturned_whenReturnConstantIsCalled_givenTypeDoesNotExist() { ci = new ConditionalInstantiator(THIS_TYPE_DOES_NOT_EXIST); Object actual = ci.returnConstant("NOPE"); - assertThat(actual).isEqualTo(null); + assertThat(actual).isNull(); } @Test @@ -210,6 +210,6 @@ void nullIsReturned_whenConstantDoesNotExist_givenFalse() { ci = new ConditionalInstantiator("java.math.BigDecimal", false); Object actual = ci.returnConstant("FORTY-TWO"); - assertThat(actual).isEqualTo(null); + assertThat(actual).isNull(); } } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/FieldIterableTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/FieldIterableTest.java index 95ca4338d..0b81f07af 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/FieldIterableTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/FieldIterableTest.java @@ -59,7 +59,7 @@ void onlySubClassFields() { @Test void noFields() { FieldIterable iterable = FieldIterable.of(NoFields.class); - assertThat(iterable.iterator().hasNext()).isFalse(); + assertThat(iterable.iterator()).isExhausted(); } @Test @@ -113,7 +113,7 @@ void orderingTest() { @Test void interfaceTest() { FieldIterable iterable = FieldIterable.of(Interface.class); - assertThat(iterable.iterator().hasNext()).isFalse(); + assertThat(iterable.iterator()).isExhausted(); } @Test @@ -129,13 +129,13 @@ void nextAfterLastElement() { @Test void objectHasNoElements() { FieldIterable iterable = FieldIterable.of(Object.class); - assertThat(iterable.iterator().hasNext()).isFalse(); + assertThat(iterable.iterator()).isExhausted(); } @Test void ignoreSyntheticFields() { FieldIterable iterable = FieldIterable.of(Outer.Inner.class); - assertThat(iterable.iterator().hasNext()).isFalse(); + assertThat(iterable.iterator()).isExhausted(); } @Test @@ -145,7 +145,7 @@ void ignoreNonSyntheticCoberturaFields() { for (FieldProbe probe : iterable) { fields.add(probe.getField()); } - assertThat(fields.size()).isEqualTo(1); + assertThat(fields).hasSize(1); assertThat(fields.get(0).getName()).isEqualTo("i"); } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/annotations/AnnotationCacheBuilderTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/annotations/AnnotationCacheBuilderTest.java index 673815098..e4adc39ea 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/annotations/AnnotationCacheBuilderTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/annotations/AnnotationCacheBuilderTest.java @@ -251,8 +251,8 @@ void annotationsArrayParametersAreFoundOnClass() { assertTypeHasAnnotation(AnnotationWithValuesContainer.class, annotation); Set annotations = new HashSet<>(annotation.properties.getArrayValues("annotations")); - assertThat(annotations.contains("javax.annotation.Nonnull")).isTrue(); - assertThat(annotations.contains("nl.jqno.equalsverifier.testhelpers.annotations.NotNull")).isTrue(); + assertThat(annotations).contains("javax.annotation.Nonnull"); + assertThat(annotations).contains("nl.jqno.equalsverifier.testhelpers.annotations.NotNull"); } @Test diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/util/ValidationsTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/util/ValidationsTest.java index 25c865cae..56ae9cb6a 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/util/ValidationsTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/util/ValidationsTest.java @@ -3,7 +3,7 @@ // CHECKSTYLE OFF: IllegalImport import static nl.jqno.equalsverifier.internal.testhelpers.Util.coverThePrivateConstructor; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.jupiter.api.Assertions.assertAll; import java.util.ArrayList;