From d0a8a1878cbb25426456f298da800242b44233aa Mon Sep 17 00:00:00 2001 From: Jan Ouwens Date: Wed, 6 Nov 2024 13:10:27 +0100 Subject: [PATCH] Tweaks ValidationTest to use ExpectedException --- .../internal/util/ValidationsTest.java | 136 +++++------------- 1 file changed, 33 insertions(+), 103 deletions(-) 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 53b31f23f..3906f1183 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 @@ -1,98 +1,67 @@ package nl.jqno.equalsverifier.internal.util; import static nl.jqno.equalsverifier.internal.testhelpers.Util.coverThePrivateConstructor; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; -import java.util.Objects; +import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; import org.junit.jupiter.api.Test; -/** Tests for {@link Validations} */ class ValidationsTest { @Test - void coverTheConstructor() { + public void coverTheConstructor() { coverThePrivateConstructor(Validations.class); } - /** - * {@link Validations#validateFieldTypeMatches(Class, String, Class)} should - * throw if the field type does not match the given type. - */ @Test - void validateFieldTypeMatches_ShouldFailOnWrongType() { + public void validateFieldTypeMatches_shouldFailOnWrongType() { assertAll( () -> { - Exception exc = assertThrows( - IllegalStateException.class, - () -> + ExpectedException + .when(() -> Validations.validateFieldTypeMatches( TestContainer.class, "listField", HashSet.class - ), - "Should not allow HashSet as a List" - ); - assertThat( - exc.getMessage(), - allOf( - containsString("should be of type List"), - containsString("but are HashSet") + ) ) - ); + .assertThrows(IllegalStateException.class) + .assertMessageContains("should be of type List", "but are HashSet"); }, () -> { - Exception exc = assertThrows( - IllegalStateException.class, - () -> + ExpectedException + .when(() -> Validations.validateFieldTypeMatches( TestContainer.class, "objectField", int.class - ), - "Should not allow primitives as an Object, because primitives are not a subtype of Object" - ); - assertThat( - exc.getMessage(), - allOf(containsString("should be of type Object"), containsString("but are int")) - ); + ) + ) + .assertThrows(IllegalStateException.class) + .assertMessageContains("should be of type Object", "but are int"); }, () -> { - Exception exc = assertThrows( - IllegalStateException.class, - () -> + ExpectedException + .when(() -> Validations.validateFieldTypeMatches( TestContainer.class, "charsField", Character.class - ), - "Should not allow Character as a CharSequence" - ); - assertThat( - exc.getMessage(), - allOf( - containsString("should be of type CharSequence"), - containsString("but are Character") + ) ) - ); + .assertThrows(IllegalStateException.class) + .assertMessageContains("should be of type CharSequence", "but are Character"); } ); } - /** - * {@link Validations#validateFieldTypeMatches(Class, String, Class)} should not - * throw if the field type is a super-type or interface of the given type. - */ @Test - void validateFieldTypeMatches_ShouldAllowSubTypes() { + public void validateFieldTypeMatches_shouldAllowSubTypes() { assertAll( () -> assertDoesNotThrow( @@ -127,80 +96,41 @@ void validateFieldTypeMatches_ShouldAllowSubTypes() { ); } - /** - * {@link Validations#validateFieldTypeMatches(Class, String, Class)} should not - * throw if the field type is a sub-type of the given type or interface. - */ @Test - void validateFieldTypeMatches_ShouldFailOnSuperTypes() { + public void validateFieldTypeMatches_shouldFailOnSuperTypes() { assertAll( () -> { - Exception exc = assertThrows( - IllegalStateException.class, - () -> + ExpectedException + .when(() -> Validations.validateFieldTypeMatches( TestContainer.class, "listField", Collection.class - ), - "Should not allow Collection as a List" - ); - assertThat( - exc.getMessage(), - allOf( - containsString("should be of type List"), - containsString("but are Collection") + ) ) - ); + .assertThrows(IllegalStateException.class) + .assertMessageContains("should be of type List", "but are Collection"); }, () -> { - Exception exc = assertThrows( - IllegalStateException.class, - () -> + ExpectedException + .when(() -> Validations.validateFieldTypeMatches( TestContainer.class, "charsField", Object.class - ), - "Should not allow Object as a CharSequence" - ); - assertThat( - exc.getMessage(), - allOf( - containsString("should be of type CharSequence"), - containsString("but are Object") + ) ) - ); + .assertThrows(IllegalStateException.class) + .assertMessageContains("should be of type CharSequence", "but are Object"); } ); } - /** Test class for type checking. */ - private static class TestContainer { + @SuppressWarnings("unused") + private static final class TestContainer { List listField; Object objectField; CharSequence charsField; - - @Override - public int hashCode() { - return Objects.hash(charsField, listField, objectField); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!(obj instanceof TestContainer)) { - return false; - } - TestContainer other = (TestContainer) obj; - return ( - Objects.equals(charsField, other.charsField) && - Objects.equals(listField, other.listField) && - Objects.equals(objectField, other.objectField) - ); - } } }