Skip to content

Commit

Permalink
Migrate all tests to JUnit Jupiter (#424)
Browse files Browse the repository at this point in the history
This change migrates all tests to use JUnit Jupiter, so that is all tests are now annotated with `org.junit.jupiter.api.Test`.

In most cases, this is a simple change of imports, with a few exceptions:
* Uses of `assertTrue` have been adjusted to allow for the description being the last parameter
* `org.hamcrest.AbstractMatcherTest` has been moved to `org.hamcrest.test.AbstractMatcherTest`
* Static assertion methods in `AbstractMatcherTest` have been moved to `org.hamcrest.test.MatcherAssertions`
  • Loading branch information
tumbarumba authored Oct 12, 2024
1 parent 246967c commit 581d6ba
Show file tree
Hide file tree
Showing 77 changed files with 543 additions and 246 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ these methods will need to be updated. The following methods are affected:
* Javadoc improvements and cleanup ([PR #420](https://github.com/hamcrest/JavaHamcrest/pull/420))
* Optional matchers ([PR #421](https://github.com/hamcrest/JavaHamcrest/pull/421))
* Derive version from git tags ([PR #419](https://github.com/hamcrest/JavaHamcrest/pull/419))
* Migrate all tests to JUnit Jupiter ([PR #424](https://github.com/hamcrest/JavaHamcrest/pull/424))

### Bugfixes

Expand Down
7 changes: 7 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format

[versions]
junit-jupiter = "5.11.2"

[libraries]
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
13 changes: 6 additions & 7 deletions hamcrest/hamcrest.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ plugins {
version = rootProject.version

dependencies {
testImplementation(group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.10.3') {
transitive = true
}
testImplementation(group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '5.10.3') {
transitive = true
exclude(module: 'hamcrest-core')
}
testImplementation libs.junit.jupiter
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named("test") {
useJUnitPlatform()
}

jar {
Expand Down
77 changes: 0 additions & 77 deletions hamcrest/src/test/java/org/hamcrest/AbstractMatcherTest.java

This file was deleted.

4 changes: 2 additions & 2 deletions hamcrest/src/test/java/org/hamcrest/BaseDescriptionTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.hamcrest;

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

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public final class BaseDescriptionTest {

Expand Down
4 changes: 2 additions & 2 deletions hamcrest/src/test/java/org/hamcrest/BaseMatcherTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.hamcrest;

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

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public final class BaseMatcherTest {

Expand Down
5 changes: 3 additions & 2 deletions hamcrest/src/test/java/org/hamcrest/CustomMatcherTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.hamcrest;

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

import static org.hamcrest.test.MatcherAssertions.assertDescription;

import static org.hamcrest.AbstractMatcherTest.assertDescription;

public final class CustomMatcherTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.hamcrest;

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

import static org.hamcrest.AbstractMatcherTest.*;
import static org.hamcrest.test.MatcherAssertions.*;

public final class CustomTypeSafeMatcherTest {

Expand Down
6 changes: 3 additions & 3 deletions hamcrest/src/test/java/org/hamcrest/FeatureMatcherTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.hamcrest;

import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.hamcrest.AbstractMatcherTest.*;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.test.MatcherAssertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

public final class FeatureMatcherTest {

Expand Down
4 changes: 2 additions & 2 deletions hamcrest/src/test/java/org/hamcrest/MatcherAssertTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.hamcrest;

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

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

public final class MatcherAssertTest {

Expand Down
4 changes: 2 additions & 2 deletions hamcrest/src/test/java/org/hamcrest/NullDescriptionTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.hamcrest;

import org.hamcrest.Description.NullDescription;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public final class NullDescriptionTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.hamcrest;

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

import static org.hamcrest.AbstractMatcherTest.*;
import static org.hamcrest.test.MatcherAssertions.*;

/**
* @author Steve Freeman 2016 http://www.hamcrest.com
Expand Down
6 changes: 3 additions & 3 deletions hamcrest/src/test/java/org/hamcrest/TypeSafeMatcherTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.hamcrest;

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

import static org.hamcrest.AbstractMatcherTest.assertMismatchDescription;
import static org.junit.Assert.assertFalse;
import static org.hamcrest.test.MatcherAssertions.assertMismatchDescription;
import static org.junit.jupiter.api.Assertions.assertFalse;

@SuppressWarnings("WeakerAccess")
public final class TypeSafeMatcherTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.hamcrest.beans;

import org.hamcrest.Matcher;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.hamcrest.AbstractMatcherTest.*;
import static org.hamcrest.test.MatcherAssertions.*;
import static org.hamcrest.beans.HasProperty.hasProperty;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

import org.hamcrest.*;
import org.hamcrest.core.IsEqual;
import org.hamcrest.test.AbstractMatcherTest;
import org.junit.jupiter.api.Test;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.beans.SimpleBeanInfo;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.test.MatcherAssertions.*;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
import static org.hamcrest.beans.HasPropertyWithValue.hasPropertyAtPath;
import static org.hamcrest.core.IsAnything.anything;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Iain McGinniss
Expand All @@ -33,36 +37,42 @@ protected Matcher<?> createMatcher() {
return hasProperty("irrelevant", anything());
}

@Test
public void testMatchesBeanWithoutInfoWithMatchedNamedProperty() {
final Matcher<BeanWithoutInfo> propertyMatcher = hasProperty("property", equalTo("is expected"));

assertMatches("with property", propertyMatcher, shouldMatch);
assertMismatchDescription("property 'property' was \"not expected\"", propertyMatcher, shouldNotMatch);
}

@Test
public void testMatchesBeanWithoutInfoWithMatchedNamedBooleanProperty() {
final Matcher<BeanWithoutInfo> booleanPropertyMatcher = hasProperty("booleanProperty", is(true));

assertMatches("with property", booleanPropertyMatcher, shouldMatch);
assertMismatchDescription("property 'booleanProperty' was <false>", booleanPropertyMatcher, shouldNotMatch);
}

@Test
public void testMatchesBeanWithInfoWithMatchedNamedProperty() {
assertMatches("with bean info", hasProperty("property", equalTo("with info")), beanWithInfo);
assertMismatchDescription("property 'property' was \"with info\"",
hasProperty("property", equalTo("without info")), beanWithInfo);
}

@Test
public void testDoesNotMatchBeanWithoutInfoOrMatchedNamedProperty() {
assertMismatchDescription("No property \"nonExistentProperty\"",
hasProperty("nonExistentProperty", anything()), shouldNotMatch);
}

@Test
public void testDoesNotMatchWriteOnlyProperty() {
assertMismatchDescription("property \"writeOnlyProperty\" is not readable",
hasProperty("writeOnlyProperty", anything()), shouldNotMatch);
}

@Test
public void testMatchesPath() {
assertMatches("1-step path", hasPropertyAtPath("property", equalTo("is expected")), shouldMatch);
assertMatches("2-step path", hasPropertyAtPath("inner.property", equalTo("is expected")), new BeanWithInner(shouldMatch));
Expand All @@ -72,31 +82,37 @@ public void testMatchesPath() {
assertMismatchDescription("inner.inner.property.was \"not expected\"", hasPropertyAtPath("inner.inner.property", equalTo("something")), new BeanWithInner(new BeanWithInner(shouldNotMatch)));
}

@Test
public void testDescribeTo() {
assertDescription("hasProperty(\"property\", <true>)", hasProperty("property", equalTo(true)));
}

@Test
public void testMatchesPropertyAndValue() {
assertMatches("property with value", hasProperty("property", anything()), beanWithInfo);
}

@Test
public void testDoesNotWriteMismatchIfPropertyMatches() {
Description description = new StringDescription();
hasProperty( "property", anything()).describeMismatch(beanWithInfo, description);
assertEquals("Expected mismatch description", "", description.toString());
assertEquals("", description.toString(), "Expected mismatch description");
}

@Test
public void testDescribesMissingPropertyMismatch() {
assertMismatchDescription("No property \"honk\"", hasProperty("honk", anything()), shouldNotMatch);
}

@Test
public void testExceptionsInBeanMethodsShouldBeReportedCorrectly() {
assertMismatchDescription(
"Calling 'public java.lang.String org.hamcrest.beans.HasPropertyWithValueTest$BeanWithBug.getBroken()': \"bean failed\"",
hasProperty("broken", anything()),
new BeanWithBug());
}

@Test
public void testCanAccessAnAnonymousInnerClass() {
class X implements IX {
@Override
Expand Down
Loading

0 comments on commit 581d6ba

Please sign in to comment.