Skip to content

Commit

Permalink
Replaces assert(DoesNot)Throw with equivalent AssertJ
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Dec 19, 2024
1 parent b34c09b commit dcca387
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package nl.jqno.equalsverifier.integration.extra_features;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException;
import org.junit.jupiter.api.Test;

// CHECKSTYLE OFF: LocalFinalVariableName
Expand All @@ -24,23 +22,24 @@ void withLombokCachedHashCode() {

@Test
void defaultEqualsVerifierFailsForCachedLombokEqualsAndHashcode() {
final AssertionError error = assertThrows(
AssertionError.class,
() -> EqualsVerifier.forClass(LazyPojo.class).suppress(Warning.STRICT_INHERITANCE).verify());
assertThat(error.getMessage()).contains("hashCode relies on $hashCodeCache, but equals does not.");
ExpectedException
.when(() -> EqualsVerifier.forClass(LazyPojo.class).suppress(Warning.STRICT_INHERITANCE).verify())
.assertFailure()
.assertMessageContains("hashCode relies on $hashCodeCache, but equals does not.");
}

@Test
void defaultEqualsVerifierFailsForCachedLombokEqualsAndHashcodeWhenUsingWithCachedHashCode() {
final IllegalArgumentException error = assertThrows(
IllegalArgumentException.class,
() -> EqualsVerifier
.forClass(LazyPojo.class)
.suppress(Warning.STRICT_INHERITANCE)
.withCachedHashCode("$hashCodeCache", "hashCode", new LazyPojo("bar", new Object()))
.verify());
assertThat(error.getMessage())
.contains("Cached hashCode: Could not find calculateHashCodeMethod: must be 'private int hashCode()'");
ExpectedException
.when(
() -> EqualsVerifier
.forClass(LazyPojo.class)
.suppress(Warning.STRICT_INHERITANCE)
.withCachedHashCode("$hashCodeCache", "hashCode", new LazyPojo("bar", new Object()))
.verify())
.assertThrows(IllegalArgumentException.class)
.assertMessageContains(
"Cached hashCode: Could not find calculateHashCodeMethod: must be 'private int hashCode()'");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package nl.jqno.equalsverifier.internal.util;

import static nl.jqno.equalsverifier.internal.testhelpers.Util.coverThePrivateConstructor;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -43,15 +43,22 @@ void validateFieldTypeMatches_shouldFailOnWrongType() {
@Test
void validateFieldTypeMatches_shouldAllowSubTypes() {
assertAll(
() -> assertDoesNotThrow(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "listField", ArrayList.class),
"Should allow ArrayList as a List"),
() -> assertDoesNotThrow(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "objectField", Integer.class),
"Should allow Integer as an Object"),
() -> assertDoesNotThrow(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "charsField", String.class),
"Should allow String as a CharSequence"));
() -> assertThatCode(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "listField", ArrayList.class))
.as("Should allow ArrayList as a List")
.doesNotThrowAnyException(),
() -> assertThatCode(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "listField", ArrayList.class))
.as("Should allow ArrayList as a List")
.doesNotThrowAnyException(),
() -> assertThatCode(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "objectField", Integer.class))
.as("Should allow Integer as an Object")
.doesNotThrowAnyException(),
() -> assertThatCode(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "charsField", String.class))
.as("Should allow String as a CharSequence")
.doesNotThrowAnyException());
}

@Test
Expand Down

0 comments on commit dcca387

Please sign in to comment.