Skip to content

Commit

Permalink
Replaces Hamcrest with AssertJ
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Dec 19, 2024
1 parent 34d992b commit a9cf82f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 60 deletions.
6 changes: 0 additions & 6 deletions equalsverifier-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@
<version>${version.junit-jupiter}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>${version.hamcrest}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nl.jqno.equalsverifier.integration.extra_features;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.StringContains.containsString;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import nl.jqno.equalsverifier.EqualsVerifier;
Expand All @@ -28,7 +27,7 @@ void defaultEqualsVerifierFailsForCachedLombokEqualsAndHashcode() {
final AssertionError error = assertThrows(
AssertionError.class,
() -> EqualsVerifier.forClass(LazyPojo.class).suppress(Warning.STRICT_INHERITANCE).verify());
assertThat(error.getMessage(), containsString("hashCode relies on $hashCodeCache, but equals does not."));
assertThat(error.getMessage()).contains("hashCode relies on $hashCodeCache, but equals does not.");
}

@Test
Expand All @@ -40,15 +39,13 @@ void defaultEqualsVerifierFailsForCachedLombokEqualsAndHashcodeWhenUsingWithCach
.suppress(Warning.STRICT_INHERITANCE)
.withCachedHashCode("$hashCodeCache", "hashCode", new LazyPojo("bar", new Object()))
.verify());
assertThat(
error.getMessage(),
containsString(
"Cached hashCode: Could not find calculateHashCodeMethod: must be 'private int hashCode()'"));
assertThat(error.getMessage())
.contains("Cached hashCode: Could not find calculateHashCodeMethod: must be 'private int hashCode()'");
}

/**
* This class has been generated with Lombok (1.18.20). It is equivalent to:
*
*
* <pre>
* &#64;RequiredArgsConstructor
* &#64;EqualsAndHashCode(cacheStrategy = EqualsAndHashCode.CacheStrategy.LAZY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package nl.jqno.equalsverifier.integration.operational;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -279,6 +277,6 @@ private void assertSuccessful(EqualsVerifierReport report, Class<?> type) {
private void assertUnsuccessful(EqualsVerifierReport report, Class<?> type, String message) {
assertThat(report.isSuccessful()).isFalse();
assertThat(report.getType()).isEqualTo(type);
assertThat(report.getMessage(), containsString(message));
assertThat(report.getMessage()).contains(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.EqualsVerifierReport;
Expand All @@ -30,9 +28,8 @@ void containsAppropriateErrorMessageAndExceptionWhenClassIsIncorrect() {

assertThat(report.getType()).isEqualTo(Point.class);
assertThat(report.isSuccessful()).isFalse();
assertThat(
report.getMessage(),
startsWith("EqualsVerifier found a problem in class nl.jqno.equalsverifier.testhelpers.types.Point"));
assertThat(report.getMessage())
.startsWith("EqualsVerifier found a problem in class nl.jqno.equalsverifier.testhelpers.types.Point");
assertThat(report.getCause().getClass()).isEqualTo(AssertionException.class);
assertThat(report.getCause().getMessage()).isNull();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nl.jqno.equalsverifier.internal.exceptions;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -47,24 +46,24 @@ void exceptionHasMessageAndCause() {

private void assertNoMessage() {
assertMessagePreamble();
assertThat(actual.getMessage(), not(containsString("\n")));
assertThat(actual.getMessage()).doesNotContain("\n");
}

private void assertMessage(String message) {
assertMessagePreamble();
assertThat(actual.getMessage(), containsString("\n"));
assertThat(actual.getMessage(), containsString(message));
assertThat(actual.getMessage()).contains("\n");
assertThat(actual.getMessage()).contains(message);
}

private void assertMessagePreamble() {
assertThat(actual.getMessage(), containsString("This is a bug in EqualsVerifier"));
assertThat(actual.getMessage()).contains("This is a bug in EqualsVerifier");
}

private void assertNoCause() {
assertThat(actual.getCause(), is(nullValue()));
assertThat(actual.getCause()).isEqualTo(null);
}

private void assertCause(Throwable cause) {
assertThat(actual.getCause(), is(cause));
assertThat(actual.getCause()).isEqualTo(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import static nl.jqno.equalsverifier.internal.reflection.Util.objects;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import java.math.BigDecimal;
import java.util.Calendar;
Expand Down Expand Up @@ -36,7 +33,7 @@ void resolveReturnsNull_whenTypeDoesntExist() {
ci = new ConditionalInstantiator(THIS_TYPE_DOES_NOT_EXIST);

Class<?> actual = ci.resolve();
assertThat(actual, is(nullValue()));
assertThat(actual).isEqualTo(null);
}

@Test
Expand All @@ -45,14 +42,14 @@ void objectIsInstantiatedCorrectly_whenValidConstructorParametersAreProvided() {
Calendar expected = new GregorianCalendar(1999, 11, 31);

Calendar actual = ci.instantiate(classes(int.class, int.class, int.class), objects(1999, 11, 31));
assertThat(actual, is(expected));
assertThat(actual).isEqualTo(expected);
}

@Test
void nullIsReturned_whenInstantiateIsCalled_givenTypeDoesNotExist() {
ci = new ConditionalInstantiator(THIS_TYPE_DOES_NOT_EXIST);
Object actual = ci.instantiate(classes(String.class), objects("nope"));
assertThat(actual, is(nullValue()));
assertThat(actual).isEqualTo(null);
}

@Test
Expand All @@ -68,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, is(nullValue()));
assertThat(actual).isEqualTo(null);
}

@Test
Expand All @@ -77,14 +74,14 @@ void objectIsInstantiatedCorrectly_whenValidFactoryMethodAndParametersAreProvide
int expected = Integer.valueOf(42);

int actual = ci.callFactory("valueOf", classes(int.class), objects(42));
assertThat(actual, is(expected));
assertThat(actual).isEqualTo(expected);
}

@Test
void nullIsReturned_whenFactoryIsCalled_givenTypeDoesNotExist() {
ci = new ConditionalInstantiator(THIS_TYPE_DOES_NOT_EXIST);
Object actual = ci.callFactory("factory", classes(String.class), objects("nope"));
assertThat(actual, is(nullValue()));
assertThat(actual).isEqualTo(null);
}

@Test
Expand All @@ -100,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, is(nullValue()));
assertThat(actual).isEqualTo(null);
}

@Test
Expand All @@ -116,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, is(nullValue()));
assertThat(actual).isEqualTo(null);
}

@Test
Expand All @@ -125,14 +122,14 @@ void objectIsInstantiatedCorrectly_whenValidExternalFactoryMethodAndParametersAr
List<?> expected = Collections.emptyList();

List<?> actual = ci.callFactory("java.util.Collections", "emptyList", classes(), objects());
assertThat(actual, is(expected));
assertThat(actual).isEqualTo(expected);
}

@Test
void nullIsReturned_whenExternalFactoryIsCalled_givenTypeDoesNotExist() {
ci = new ConditionalInstantiator(THIS_TYPE_DOES_NOT_EXIST);
Object actual = ci.callFactory("java.util.Collections", "emptyList", classes(), objects());
assertThat(actual, is(nullValue()));
assertThat(actual).isEqualTo(null);
}

@Test
Expand All @@ -148,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, is(nullValue()));
assertThat(actual).isEqualTo(null);
}

@Test
Expand All @@ -165,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, is(nullValue()));
assertThat(actual).isEqualTo(null);
}

@Test
Expand All @@ -182,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, is(nullValue()));
assertThat(actual).isEqualTo(null);
}

@Test
void nullIsReturned_whenReturnConstantIsCalled_givenTypeDoesNotExist() {
ci = new ConditionalInstantiator(THIS_TYPE_DOES_NOT_EXIST);
Object actual = ci.returnConstant("NOPE");
assertThat(actual, is(nullValue()));
assertThat(actual).isEqualTo(null);
}

@Test
Expand All @@ -198,7 +195,7 @@ void objectIsReturned_whenValidConstantIsProvided() {
BigDecimal expected = BigDecimal.TEN;

BigDecimal actual = ci.returnConstant("TEN");
assertThat(actual, is(expected));
assertThat(actual).isEqualTo(expected);
}

@Test
Expand All @@ -213,6 +210,6 @@ void nullIsReturned_whenConstantDoesNotExist_givenFalse() {
ci = new ConditionalInstantiator("java.math.BigDecimal", false);

Object actual = ci.returnConstant("FORTY-TWO");
assertThat(actual, is(nullValue()));
assertThat(actual).isEqualTo(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;

import nl.jqno.equalsverifier.internal.reflection.Instantiator;
import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException;
Expand Down Expand Up @@ -60,28 +58,28 @@ void oneParameterWithNoFieldsAndThrowsWithNullMessage() {
void oneAbstractParameter() {
Instantiator<Abstract> i = Instantiator.of(Abstract.class, objenesis);
Formatter f = Formatter.of("Abstract: %%", i.instantiate());
assertThat(f.format(), containsString("Abstract: [Abstract x=0]"));
assertThat(f.format()).contains("Abstract: [Abstract x=0]");
}

@Test
void oneConcreteSubclassParameter() {
Instantiator<AbstractImpl> i = Instantiator.of(AbstractImpl.class, objenesis);
Formatter f = Formatter.of("Concrete: %%", i.instantiate());
assertThat(f.format(), containsString("Concrete: something concrete"));
assertThat(f.format()).contains("Concrete: something concrete");
}

@Test
void oneDelegatedAbstractParameter() {
Instantiator<AbstractDelegation> i = Instantiator.of(AbstractDelegation.class, objenesis);
Formatter f = Formatter.of("Abstract: %%", i.instantiate());
assertThat(f.format(), containsString("Abstract: [AbstractDelegation y=0]"));
assertThat(f.format()).contains("Abstract: [AbstractDelegation y=0]");
}

@Test
void oneDelegatedConcreteSubclassParameter() {
Instantiator<AbstractDelegationImpl> i = Instantiator.of(AbstractDelegationImpl.class, objenesis);
Formatter f = Formatter.of("Concrete: %%", i.instantiate());
assertThat(f.format(), containsString("Concrete: something concrete"));
assertThat(f.format()).contains("Concrete: something concrete");
}

@Test
Expand All @@ -91,7 +89,7 @@ void oneThrowingContainerParameter() {
Formatter f = Formatter.of("TC: %%", tc);
String expected =
"TC: [ThrowingContainer t=[Throwing i=0 s=null]-throws IllegalStateException(msg)]-throws IllegalStateException(msg)";
assertThat(f.format(), containsString(expected));
assertThat(f.format()).contains(expected);
}

@Test
Expand All @@ -100,7 +98,7 @@ void oneAbstractContainerParameter() {
AbstractContainer ac = new AbstractContainer(i.instantiate());

Formatter f = Formatter.of("AC: %%", ac);
assertThat(f.format(), containsString("AC: [AbstractContainer ad=[AbstractDelegation y=0]]"));
assertThat(f.format()).contains("AC: [AbstractContainer ad=[AbstractDelegation y=0]]");
}

@Test
Expand All @@ -112,13 +110,13 @@ void parameterWithMixOfVariousFields() {
String expected =
"[Mix i=42 s=null t=not null throwing=[Throwing i=42 s=empty]-throws IllegalStateException(msg)]"
+ "-throws UnsupportedOperationException(null)";
assertThat(f.format(), containsString(expected));
assertThat(f.format()).contains(expected);
}

@Test
void connectedParameters() {
Formatter f = Formatter.of("%%%%", 1, 2);
assertThat(f.format(), containsString("12"));
assertThat(f.format()).contains("12");
}

@Test
Expand Down
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
<version.eclipse-annotation>2.3.100</version.eclipse-annotation>
<version.equalsverifier-signedjar-test>0.2</version.equalsverifier-signedjar-test>
<version.guava>33.3.1-jre</version.guava>
<version.hamcrest>3.0</version.hamcrest>
<version.jakarta-persistence-api>3.0.0</version.jakarta-persistence-api>
<version.javax-annotation-api>1.3.2</version.javax-annotation-api>
<version.jcip-annotations>1.0</version.jcip-annotations>
Expand Down

0 comments on commit a9cf82f

Please sign in to comment.