Skip to content

Commit

Permalink
Introduces AssertJ to replace JUnit assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Dec 19, 2024
1 parent 6ec9bc6 commit 34d992b
Show file tree
Hide file tree
Showing 143 changed files with 1,825 additions and 1,818 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,40 @@

// CHECKSTYLE OFF: ParameterNumber

public class JavaFxClassesTest {
class JavaFxClassesTest {

@Test
public void successfullyInstantiatesAJavaFxClass_whenJavaFxIsAvailable() {
void successfullyInstantiatesAJavaFxClass_whenJavaFxIsAvailable() {
EqualsVerifier.forClass(JavaFxApiClassesContainer.class).verify();
}

@Test
public void succeed_whenEqualsLooksAtObservableListFieldsGenericContent() {
void succeed_whenEqualsLooksAtObservableListFieldsGenericContent() {
EqualsVerifier.forClass(JavaFxObservableListContainer.class).verify();
}

@Test
public void succeed_whenEqualsLooksAtObservableMapFieldsGenericContent() {
void succeed_whenEqualsLooksAtObservableMapFieldsGenericContent() {
EqualsVerifier.forClass(JavaFxObservableMapContainer.class).verify();
}

@Test
public void succeed_whenEqualsLooksAtObservableSetFieldsGenericContent() {
void succeed_whenEqualsLooksAtObservableSetFieldsGenericContent() {
EqualsVerifier.forClass(JavaFxObservableSetContainer.class).verify();
}

@Test
public void succeed_whenEqualsLooksAtListPropertyFieldsGenericContent() {
void succeed_whenEqualsLooksAtListPropertyFieldsGenericContent() {
EqualsVerifier.forClass(JavaFxListPropertyContainer.class).verify();
}

@Test
public void succeed_whenEqualsLooksAtMapPropertyFieldsGenericContent() {
void succeed_whenEqualsLooksAtMapPropertyFieldsGenericContent() {
EqualsVerifier.forClass(JavaFxMapPropertyContainer.class).verify();
}

@Test
public void succeed_whenEqualsLooksAtSetPropertyFieldsGenericContent() {
void succeed_whenEqualsLooksAtSetPropertyFieldsGenericContent() {
EqualsVerifier.forClass(JavaFxSetPropertyContainer.class).verify();
}

Expand Down
6 changes: 6 additions & 0 deletions equalsverifier-16/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@
<version>${version.junit-jupiter}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${version.assertj}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import nl.jqno.equalsverifier.internal.testhelpers.Util;
import org.junit.jupiter.api.Test;

public class InstanceofPatternMatchTest {
class InstanceofPatternMatchTest {

@Test
void succeed_whenEqualsUsesInstanceofPatternMatch() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@
import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException;
import org.junit.jupiter.api.Test;

public class PrefabValuesForFieldInRecordTest {
class PrefabValuesForFieldInRecordTest {

@Test
public void fail_whenRecordHasSinglePrecondition() {
void fail_whenRecordHasSinglePrecondition() {
ExpectedException
.when(() -> EqualsVerifier.forClass(SinglePrecondition.class).verify())
.assertFailure()
.assertMessageContains("Record:", "failed to run constructor", "[1]");
}

@Test
public void succeed_whenRecordHasSinglePrecondition_givenPrefabValuesForField() {
void succeed_whenRecordHasSinglePrecondition_givenPrefabValuesForField() {
EqualsVerifier.forClass(SinglePrecondition.class).withPrefabValuesForField("i", 111, 142).verify();
}

@Test
public void fail_whenRecordHasDualPrecondition() {
void fail_whenRecordHasDualPrecondition() {
ExpectedException
.when(() -> EqualsVerifier.forClass(DualPrecondition.class).verify())
.assertFailure()
.assertMessageContains("Record:", "failed to run constructor", "[1, 1]");
}

@Test
public void fail_whenRecordHasDualPrecondition_givenPrefabValuesForOnlyOneField() {
void fail_whenRecordHasDualPrecondition_givenPrefabValuesForOnlyOneField() {
ExpectedException
.when(
() -> EqualsVerifier
Expand All @@ -40,7 +40,7 @@ public void fail_whenRecordHasDualPrecondition_givenPrefabValuesForOnlyOneField(
}

@Test
public void succeed_whenRecordHasDualPrecondition_givenPrefabValueForBothFields() {
void succeed_whenRecordHasDualPrecondition_givenPrefabValueForBothFields() {
EqualsVerifier
.forClass(DualPrecondition.class)
.withPrefabValuesForField("x", 111, 142)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package nl.jqno.equalsverifier.integration.extended_contract;

import static nl.jqno.equalsverifier.internal.testhelpers.Util.defaultEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.Serializable;
import java.util.Objects;
Expand All @@ -13,129 +12,129 @@
import nl.jqno.equalsverifier.internal.versionspecific.RecordsHelper;
import org.junit.jupiter.api.Test;

public class RecordsTest {
class RecordsTest {

@Test
public void sanityCheckForRecordsHelper() {
assertTrue(RecordsHelper.isRecord(SimpleRecord.class));
assertFalse(RecordsHelper.isRecord(String.class));
void sanityCheckForRecordsHelper() {
assertThat(RecordsHelper.isRecord(SimpleRecord.class)).isTrue();
assertThat(RecordsHelper.isRecord(String.class)).isFalse();
}

@Test
public void succeed_whenClassIsARecord() {
void succeed_whenClassIsARecord() {
EqualsVerifier.forClass(SimpleRecord.class).verify();
}

@Test
public void succeed_whenClassIsAPrivateRecord() {
void succeed_whenClassIsAPrivateRecord() {
EqualsVerifier.forClass(PrivateSimpleRecord.class).verify();
}

@Test
public void fail_whenConstructorChecksNull() {
void fail_whenConstructorChecksNull() {
ExpectedException
.when(() -> EqualsVerifier.forClass(NullCheckingRecord.class).verify())
.assertFailure()
.assertMessageContains("Record:", "failed to run constructor", "Warning.NULL_FIELDS");
}

@Test
public void succeed_whenConstructorChecksNull_givenSuppressedWarning() {
void succeed_whenConstructorChecksNull_givenSuppressedWarning() {
EqualsVerifier.forClass(NullCheckingRecord.class).suppress(Warning.NULL_FIELDS).verify();
}

@Test
public void fail_whenConstructorChecksValue() {
void fail_whenConstructorChecksValue() {
ExpectedException
.when(() -> EqualsVerifier.forClass(ValueCheckingRecord.class).verify())
.assertFailure()
.assertMessageContains("Record:", "failed to run constructor", "prefab values");
}

@Test
public void fail_whenRecordInvariantIsViolated_givenIntFieldIsModifiedInConstructor() {
void fail_whenRecordInvariantIsViolated_givenIntFieldIsModifiedInConstructor() {
ExpectedException
.when(() -> EqualsVerifier.forClass(BrokenInvariantIntFieldRecord.class).verify())
.assertFailure()
.assertMessageContains("Record invariant", "intField");
}

@Test
public void fail_whenRecordInvariantIsViolated_givenStringFieldIsModifiedInConstructor() {
void fail_whenRecordInvariantIsViolated_givenStringFieldIsModifiedInConstructor() {
ExpectedException
.when(() -> EqualsVerifier.forClass(BrokenInvariantStringFieldRecord.class).verify())
.assertFailure()
.assertMessageContains("Record invariant", "stringField");
}

@Test
public void fail_whenRecordInvariantIsViolated_givenBothFieldsAreModifiedInConstructor() {
void fail_whenRecordInvariantIsViolated_givenBothFieldsAreModifiedInConstructor() {
ExpectedException
.when(() -> EqualsVerifier.forClass(BrokenInvariantBothRecord.class).verify())
.assertFailure()
.assertMessageContains("Record invariant", "intField", "stringField");
}

@Test
public void succeed_whenRecordImplementsItsOwnEquals() {
void succeed_whenRecordImplementsItsOwnEquals() {
EqualsVerifier.forClass(EqualsRecord.class).verify();
}

@Test
public void fail_whenRecordImplementsItsOwnEquals_givenNotAllFieldsAreUsed() {
void fail_whenRecordImplementsItsOwnEquals_givenNotAllFieldsAreUsed() {
ExpectedException
.when(() -> EqualsVerifier.forClass(NotAllFieldsRecord.class).verify())
.assertFailure()
.assertMessageContains("Significant fields");
}

@Test
public void fail_whenRecordConstructorThrows() {
void fail_whenRecordConstructorThrows() {
ExpectedException
.when(() -> EqualsVerifier.forClass(ThrowingConstructorRecord.class).verify())
.assertFailure()
.assertMessageContains("Record", "failed to run constructor");
}

@Test
public void fail_whenRecordConstructorThrowsNpe() {
void fail_whenRecordConstructorThrowsNpe() {
ExpectedException
.when(() -> EqualsVerifier.forClass(NullFieldRecord.class).verify())
.assertFailure()
.assertMessageContains("Record", "failed to run constructor");
}

@Test
public void fail_whenRecordAccessorThrows() {
void fail_whenRecordAccessorThrows() {
ExpectedException
.when(() -> EqualsVerifier.forClass(ThrowingAccessorRecord.class).verify())
.assertFailure()
.assertMessageContains("Record", "failed to run accessor method");
}

@Test
public void fail_whenRecordAccessorThrowsNpe() {
void fail_whenRecordAccessorThrowsNpe() {
ExpectedException
.when(() -> EqualsVerifier.forClass(NullAccessorRecord.class).verify())
.assertFailure()
.assertMessageContains("Record", "failed to run accessor method", "s()");
}

@Test
public void succeed_whenRecordContainsStaticField() {
void succeed_whenRecordContainsStaticField() {
EqualsVerifier.forClass(StaticFieldRecord.class).verify();
}

@Test
public void succeed_whenRecordValidatesInput_givenValidPrefabValues() {
void succeed_whenRecordValidatesInput_givenValidPrefabValues() {
EqualsVerifier
.forClass(ValidatingConstructorRecord.class)
.withPrefabValues(String.class, "valid-1", "valid-2")
.verify();
}

@Test
public void succeed_whenRecordHasBoundedWildcardGeneric() {
void succeed_whenRecordHasBoundedWildcardGeneric() {
EqualsVerifier.forClass(WildcardGenericRecordContainer.class).verify();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package nl.jqno.equalsverifier.internal.instantiation;

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

import java.lang.reflect.Field;
import java.util.HashMap;
Expand All @@ -10,10 +10,10 @@
import org.junit.jupiter.api.Test;
import org.objenesis.ObjenesisStd;

public class RecordInstanceCreatorTest {
class RecordInstanceCreatorTest {

@Test
public void instanceCreator() throws NoSuchFieldException {
void instanceCreator() throws NoSuchFieldException {
ClassProbe<SomeRecord> probe = ClassProbe.of(SomeRecord.class);
InstanceCreator<SomeRecord> sut = new InstanceCreator<>(probe, new ObjenesisStd());

Expand All @@ -25,9 +25,9 @@ public void instanceCreator() throws NoSuchFieldException {

SomeRecord actual = sut.instantiate(values);

assertEquals(42, actual.x);
assertEquals(0, actual.y);
assertEquals("42", actual.z);
assertThat(actual.x).isEqualTo(42);
assertThat(actual.y).isEqualTo(0);
assertThat(actual.z).isEqualTo("42");
}

record SomeRecord(int x, int y, String z) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories;

import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotSame;

import java.util.LinkedHashSet;
Expand All @@ -15,14 +15,14 @@
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;

public class RecordFallbackFactoryTest {
class RecordFallbackFactoryTest {

private FallbackFactory<?> factory;
private VintageValueProvider valueProvider;
private LinkedHashSet<TypeTag> typeStack;

@BeforeEach
public void setUp() {
void setUp() {
Objenesis objenesis = new ObjenesisStd();
factory = new FallbackFactory<>(objenesis);
FactoryCache factoryCache = new FactoryCache();
Expand All @@ -32,10 +32,10 @@ public void setUp() {
}

@Test
public void redCopyHasTheSameValuesAsRed_whenSutContainsGenericValueThatNeedsToBeIdenticalInRedAndRedCopy() {
void redCopyHasTheSameValuesAsRed_whenSutContainsGenericValueThatNeedsToBeIdenticalInRedAndRedCopy() {
Tuple<?> tuple = factory.createValues(new TypeTag(GenericRecordContainer.class), valueProvider, typeStack);

assertEquals(tuple.getRed(), tuple.getRedCopy());
assertThat(tuple.getRedCopy()).isEqualTo(tuple.getRed());
assertNotSame(tuple.getRed(), tuple.getRedCopy());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;

import nl.jqno.equalsverifier.internal.reflection.FieldIterable;
import nl.jqno.equalsverifier.internal.reflection.FieldProbe;
Expand All @@ -11,21 +10,21 @@
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;

public class RecordObjectAccessorCopyingTest {
class RecordObjectAccessorCopyingTest {

private Objenesis objenesis = new ObjenesisStd();

@Test
public void copyHappyPath() {
void copyHappyPath() {
Object original = instantiate(SimpleRecord.class);
Object copy = copyOf(original);

assertNotSame(original, copy);
assertEquals(original, copy);
assertThat(copy).isEqualTo(original);
}

@Test
public void shallowCopy() {
void shallowCopy() {
Object original = instantiate(RecordContainer.class);
Object copy = copyOf(original);
RecordObjectAccessor<?> originalAccessor = create(original);
Expand All @@ -35,7 +34,7 @@ public void shallowCopy() {
for (FieldProbe p : FieldIterable.of(original.getClass())) {
Object a = originalAccessor.getField(p);
Object b = copyAccessor.getField(p);
assertSame(a, b);
assertThat(b).isSameAs(a);
}
}

Expand Down
Loading

0 comments on commit 34d992b

Please sign in to comment.