Skip to content

Commit

Permalink
Convert test base class to custom assertion to promote best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Nov 26, 2024
1 parent c1a976e commit e3f2a09
Show file tree
Hide file tree
Showing 24 changed files with 52 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
import static org.assertj.core.api.Assertions.assertThat;

/**
* Abstract base class for unit tests that wish to test
* Assertions for unit tests that wish to test
* {@link Object#equals(Object)} and {@link Object#hashCode()}.
*
* @since 1.3
*/
public abstract class AbstractEqualsAndHashCodeTests {
public class EqualsAndHashCodeAssertions {

protected final <T> void assertEqualsAndHashCode(T equal1, T equal2, T different) {
private EqualsAndHashCodeAssertions() {
}

@SuppressWarnings("EqualsWithItself")
public static <T> void assertEqualsAndHashCode(T equal1, T equal2, T different) {
assertThat(equal1).isNotNull();
assertThat(equal2).isNotNull();
assertThat(different).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
package org.junit.jupiter.api.parallel;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;
import static org.junit.jupiter.api.parallel.ResourceAccessMode.READ;
import static org.junit.jupiter.api.parallel.ResourceAccessMode.READ_WRITE;
import static org.junit.jupiter.api.parallel.ResourceLocksProvider.Lock;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import org.junit.jupiter.api.Test;

/**
* Unit tests for {@link Lock}.
*
* @since 5.12
*/
class LockTests extends AbstractEqualsAndHashCodeTests {
class LockTests {

@Test
void readWriteModeSetByDefault() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

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

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.PreconditionViolationException;

Expand All @@ -23,7 +23,7 @@
* @since 1.3
* @see DiscoverySelectorsTests
*/
class ClassSelectorTests extends AbstractEqualsAndHashCodeTests {
class ClassSelectorTests {

@Test
void equalsAndHashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

package org.junit.platform.engine.discovery;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;

import org.junit.jupiter.api.Test;

/**
Expand All @@ -19,7 +20,7 @@
* @since 1.3
* @see DiscoverySelectorsTests
*/
class ClasspathResourceSelectorTests extends AbstractEqualsAndHashCodeTests {
class ClasspathResourceSelectorTests {

@Test
void equalsAndHashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

package org.junit.platform.engine.discovery;

import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;

import java.net.URI;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -21,7 +22,7 @@
* @since 1.3
* @see DiscoverySelectorsTests
*/
class ClasspathRootSelectorTests extends AbstractEqualsAndHashCodeTests {
class ClasspathRootSelectorTests {

@Test
void equalsAndHashCode() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

package org.junit.platform.engine.discovery;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;

import org.junit.jupiter.api.Test;

/**
Expand All @@ -19,7 +20,7 @@
* @since 1.3
* @see DiscoverySelectorsTests
*/
class DirectorySelectorTests extends AbstractEqualsAndHashCodeTests {
class DirectorySelectorTests {

@Test
void equalsAndHashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.util.stream.Stream;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -31,7 +31,7 @@
* @since 1.7
*/
@DisplayName("FilePosition unit tests")
class FilePositionTests extends AbstractEqualsAndHashCodeTests {
class FilePositionTests {

@Test
@DisplayName("factory method preconditions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

package org.junit.platform.engine.discovery;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;

import org.junit.jupiter.api.Test;

/**
Expand All @@ -19,7 +20,7 @@
* @since 1.3
* @see DiscoverySelectorsTests
*/
class FileSelectorTests extends AbstractEqualsAndHashCodeTests {
class FileSelectorTests {

@Test
void equalsAndHashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;

import java.util.stream.Stream;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.PreconditionViolationException;
Expand All @@ -26,7 +26,7 @@
* @since 1.3
* @see DiscoverySelectorsTests
*/
class MethodSelectorTests extends AbstractEqualsAndHashCodeTests {
class MethodSelectorTests {

private static final String TEST_CASE_NAME = TestCase.class.getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

package org.junit.platform.engine.discovery;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;

import org.junit.jupiter.api.Test;

/**
Expand All @@ -19,7 +20,7 @@
* @since 1.3
* @see DiscoverySelectorsTests
*/
class ModuleSelectorTests extends AbstractEqualsAndHashCodeTests {
class ModuleSelectorTests {

@Test
void equalsAndHashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

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

import java.util.List;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.PreconditionViolationException;

Expand All @@ -25,7 +25,7 @@
* @since 1.6
* @see DiscoverySelectorsTests
*/
class NestedClassSelectorTests extends AbstractEqualsAndHashCodeTests {
class NestedClassSelectorTests {

@Test
void equalsAndHashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

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

import java.util.List;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.PreconditionViolationException;

Expand All @@ -25,7 +25,7 @@
* @since 1.6
* @see DiscoverySelectorsTests
*/
class NestedMethodSelectorTests extends AbstractEqualsAndHashCodeTests {
class NestedMethodSelectorTests {

@Test
void equalsAndHashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

package org.junit.platform.engine.discovery;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;

import org.junit.jupiter.api.Test;

/**
Expand All @@ -19,7 +20,7 @@
* @since 1.3
* @see DiscoverySelectorsTests
*/
class PackageSelectorTests extends AbstractEqualsAndHashCodeTests {
class PackageSelectorTests {

@Test
void equalsAndHashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

package org.junit.platform.engine.discovery;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;

import org.junit.jupiter.api.Test;
import org.junit.platform.engine.UniqueId;

Expand All @@ -20,7 +21,7 @@
* @since 1.3
* @see DiscoverySelectorsTests
*/
class UniqueIdSelectorTests extends AbstractEqualsAndHashCodeTests {
class UniqueIdSelectorTests {

@Test
void equalsAndHashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

package org.junit.platform.engine.discovery;

import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;

import java.net.URI;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -21,7 +22,7 @@
* @since 1.3
* @see DiscoverySelectorsTests
*/
class UriSelectorTests extends AbstractEqualsAndHashCodeTests {
class UriSelectorTests {

@Test
void equalsAndHashCode() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.Serializable;
import java.util.stream.Stream;

import org.junit.jupiter.api.AbstractEqualsAndHashCodeTests;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.platform.engine.TestSource;
Expand All @@ -34,7 +33,7 @@
*
* @since 1.0
*/
abstract class AbstractTestSourceTests extends AbstractEqualsAndHashCodeTests {
abstract class AbstractTestSourceTests {

abstract Stream<? extends Serializable> createSerializableInstances() throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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

import java.io.Serializable;
import java.net.URI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;
import static org.junit.platform.engine.support.descriptor.ClasspathResourceSource.CLASSPATH_SCHEME;

import java.net.URI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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

import java.io.File;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;

import java.net.URI;
import java.util.stream.Stream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.util.stream.Stream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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

import java.io.File;
import java.util.stream.Stream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;

import java.io.Serializable;
import java.lang.reflect.Method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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

import java.io.Serializable;
import java.util.stream.Stream;
Expand Down

0 comments on commit e3f2a09

Please sign in to comment.