-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for JUnit 4, JUnit 5 and hybrid JUnit4/5 situations
- Loading branch information
1 parent
6e3f583
commit 017c469
Showing
18 changed files
with
526 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
hamcrest-junit4-junit5-tests/hamcrest-junit4-junit5-tests.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
group 'org.hamcrest' | ||
version '2.3-SNAPSHOT' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
testImplementation project(':hamcrest') | ||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' | ||
testImplementation 'org.junit.vintage:junit-vintage-engine:5.8.2' | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
} |
54 changes: 54 additions & 0 deletions
54
hamcrest-junit4-junit5-tests/src/test/java/org/hamcrest/JUnit4MatcherAssumeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.hamcrest; | ||
|
||
import org.junit.Test; | ||
import org.junit.AssumptionViolatedException; | ||
import org.opentest4j.TestAbortedException; | ||
|
||
import static org.hamcrest.MatcherAssume.assumeThat; | ||
import static org.hamcrest.Matchers.startsWith; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
/** | ||
* Tests compatibility with JUnit 4 <i>and</i> JUnit 5 on the classpath. | ||
* The equivalent test with only JUnit 4 on the classpath is in another module. | ||
*/ | ||
public class JUnit4MatcherAssumeTest { | ||
|
||
@Test public void | ||
assumptionFailsWithMessage() { | ||
try { | ||
assumeThat("Custom assumption", "a", startsWith("abc")); | ||
fail("should have failed"); | ||
} | ||
catch (AssumptionViolatedException e) { | ||
assertEquals("Custom assumption: got: \"a\", expected: a string starting with \"abc\"", e.getMessage()); | ||
} | ||
catch (TestAbortedException e) { | ||
throw new AssertionError("Illegal JUnit 5 assumption", e); | ||
} | ||
} | ||
|
||
@Test public void | ||
assumptionFailsWithDefaultMessage() { | ||
try { | ||
assumeThat("a", startsWith("abc")); | ||
fail("should have failed"); | ||
} | ||
catch (AssumptionViolatedException e) { | ||
assertEquals(": got: \"a\", expected: a string starting with \"abc\"", e.getMessage()); | ||
} | ||
catch (TestAbortedException e) { | ||
throw new AssertionError("Illegal JUnit 5 assumption", e); | ||
} | ||
} | ||
|
||
@Test public void | ||
assumptionSucceeds() { | ||
try { | ||
assumeThat("xyz", startsWith("xy")); | ||
} catch (TestAbortedException e) { | ||
throw new AssertionError("Illegal JUnit 5 assumption", e); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
hamcrest-junit4-junit5-tests/src/test/java/org/hamcrest/JUnit5MatcherAssumeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.hamcrest; | ||
|
||
import org.junit.AssumptionViolatedException; | ||
import org.junit.jupiter.api.Test; | ||
import org.opentest4j.TestAbortedException; | ||
|
||
import static org.hamcrest.MatcherAssume.assumeThat; | ||
import static org.hamcrest.Matchers.startsWith; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
/** | ||
* Tests compatibility with JUnit 5 with JUnit 4 <i>and</i> JUnit 5 on the classpath. | ||
* The equivalent test with only JUnit 4 on the classpath is in another module. | ||
*/ | ||
class JUnit5MatcherAssumeTest { | ||
|
||
@Test | ||
void | ||
assumptionFailsWithMessage() { | ||
try { | ||
assumeThat("Custom assumption", "a", startsWith("abc")); | ||
fail("should have failed"); | ||
} | ||
catch (TestAbortedException e) { | ||
assertEquals("Assumption failed: Custom assumption", e.getMessage()); | ||
} | ||
catch (AssumptionViolatedException e) { | ||
// If we don't catch JUnit 4 exceptions here, then this test will result in a false positive, or actually | ||
// a false ignored test. | ||
throw new AssertionError("Illegal JUnit 4 assumption", e); | ||
} | ||
} | ||
|
||
@Test void | ||
assumptionFailsWithDefaultMessage() { | ||
try { | ||
assumeThat("a", startsWith("abc")); | ||
fail("should have failed"); | ||
} | ||
catch (TestAbortedException e) { | ||
assertEquals("Assumption failed", e.getMessage()); | ||
} | ||
catch (AssumptionViolatedException e) { | ||
// If we don't catch JUnit 4 exceptions here, then this test will result in a false positive, or actually | ||
// a false ignored test. | ||
throw new AssertionError("Illegal JUnit 4 assumption", e); | ||
} | ||
} | ||
|
||
@Test void | ||
assumptionSucceeds() { | ||
try { | ||
assumeThat("xyz", startsWith("xy")); | ||
} | ||
catch (AssumptionViolatedException e) { | ||
// If we don't catch JUnit 4 exceptions here, then this test will result in a false positive, or actually | ||
// a false ignored test. | ||
throw new AssertionError("Illegal JUnit 4 assumption", e); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
hamcrest-junit4-junit5-tests/src/test/java/org/hamcrest/MatcherAssumeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.hamcrest; | ||
|
||
import org.junit.AssumptionViolatedException; | ||
import org.junit.jupiter.api.Test; | ||
import org.opentest4j.TestAbortedException; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
|
||
import static java.util.concurrent.Executors.newSingleThreadExecutor; | ||
import static org.hamcrest.MatcherAssume.assumeThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
class MatcherAssumeTest { | ||
|
||
@Test | ||
void assumptionFailsWithAssertionErrorWhenNoJUnitInStackTrace() throws Throwable { | ||
// Run the assumption on a separate thread to make sure it has JUnit 4 nor JUnit 5 in its stack trace. | ||
ExecutorService executor = newSingleThreadExecutor(); | ||
try { | ||
try { | ||
executor.submit(new Runnable() { | ||
|
||
@Override | ||
public void run() { | ||
assumeThat(1, is(2)); | ||
} | ||
}).get(); | ||
fail("Expected " + ExecutionException.class); | ||
} catch (ExecutionException expected) { | ||
throw expected.getCause(); | ||
} | ||
} catch (AssertionError expected) { | ||
} catch (TestAbortedException | AssumptionViolatedException e) { | ||
throw new AssertionError(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
dependencies { | ||
testImplementation project(':hamcrest') | ||
testImplementation(group: 'junit', name: 'junit', version: '4.13.2') { | ||
transitive = false | ||
} | ||
} | ||
|
||
jar { | ||
manifest { | ||
attributes 'Implementation-Title': project.name, | ||
'Implementation-Vendor': 'hamcrest.org', | ||
'Implementation-Version': version, | ||
'Automatic-Module-Name': 'org.hamcrest.junit4-tests' | ||
} | ||
} | ||
|
||
javadoc.title = "Hamcrest JUnit 4 Tests $version" |
43 changes: 43 additions & 0 deletions
43
hamcrest-junit4-tests/src/test/java/org/hamcrest/JUnit4MatcherAssumeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.hamcrest; | ||
|
||
import org.junit.Test; | ||
import org.junit.AssumptionViolatedException; | ||
|
||
import static org.hamcrest.MatcherAssume.assumeThat; | ||
import static org.hamcrest.Matchers.startsWith; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
/** | ||
* Tests compatibility with JUnit 4 with only JUnit 4 on the classpath. | ||
* The equivalent test with JUnit 4 <i>and</i> JUnit 5 on the classpath is in another module. | ||
*/ | ||
public class JUnit4MatcherAssumeTest { | ||
|
||
@Test public void | ||
assumptionFailsWithMessage() { | ||
try { | ||
assumeThat("Custom assumption", "a", startsWith("abc")); | ||
fail("should have failed"); | ||
} | ||
catch (AssumptionViolatedException e) { | ||
assertEquals("Custom assumption: got: \"a\", expected: a string starting with \"abc\"", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test public void | ||
assumptionFailsWithDefaultMessage() { | ||
try { | ||
assumeThat("a", startsWith("abc")); | ||
fail("should have failed"); | ||
} | ||
catch (AssumptionViolatedException e) { | ||
assertEquals(": got: \"a\", expected: a string starting with \"abc\"", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test public void | ||
assumptionSucceeds() { | ||
assumeThat("xyz", startsWith("xy")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
dependencies { | ||
api project(':hamcrest') | ||
api 'org.opentest4j:opentest4j:1.2.0' | ||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' | ||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2' | ||
} | ||
|
||
jar { | ||
manifest { | ||
attributes 'Implementation-Title': project.name, | ||
'Implementation-Vendor': 'hamcrest.org', | ||
'Implementation-Version': version, | ||
'Automatic-Module-Name': 'org.hamcrest.junit5-tests' | ||
} | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
} | ||
|
||
javadoc.title = "Hamcrest JUnit 5 Tests $version" |
44 changes: 44 additions & 0 deletions
44
hamcrest-junit5-tests/src/test/java/org/hamcrest/JUnit5MatcherAssumeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.hamcrest; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.opentest4j.TestAbortedException; | ||
|
||
import static org.hamcrest.MatcherAssume.assumeThat; | ||
import static org.hamcrest.Matchers.startsWith; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
/** | ||
* Tests compatibility with JUnit 5 with only JUnit 5 on the classpath. | ||
* The equivalent test with JUnit 4 <i>and</i> JUnit 5 on the classpath is in another module. | ||
*/ | ||
class JUnit5MatcherAssumeTest { | ||
|
||
@Test | ||
void | ||
assumptionFailsWithMessage() { | ||
try { | ||
assumeThat("Custom assumption", "a", startsWith("abc")); | ||
fail("should have failed"); | ||
} | ||
catch (TestAbortedException e) { | ||
assertEquals("Assumption failed: Custom assumption", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test void | ||
assumptionFailsWithDefaultMessage() { | ||
try { | ||
assumeThat("a", startsWith("abc")); | ||
fail("should have failed"); | ||
} | ||
catch (TestAbortedException e) { | ||
assertEquals("Assumption failed", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test void | ||
assumptionSucceeds() { | ||
assumeThat("xyz", startsWith("xy")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.