Skip to content

Commit 1db1677

Browse files
committed
hamcrest#322 Added MatcherAssume.assumeThat again
1 parent c6ddf29 commit 1db1677

18 files changed

+572
-1
lines changed

build.gradle

+36
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,39 @@ publishing {
129129
'Hamcrest Library',
130130
'A library of Hamcrest matchers - deprecated, please use "hamcrest" instead')
131131
}
132+
133+
def hamcrestJUnit5TestsProject = project(':hamcrest-junit5-tests')
134+
hamcrestJUnit5Tests(MavenPublication) {
135+
from hamcrestJUnit5TestsProject.components.java
136+
artifactId hamcrestJUnit5TestsProject.name
137+
artifact hamcrestJUnit5TestsProject.sourcesJar
138+
artifact hamcrestJUnit5TestsProject.javadocJar
139+
pom pomConfigurationFor(
140+
'Hamcrest JUnit 5 Tests',
141+
'A test suite for Hamcrest assumptions using JUnit 5')
142+
}
143+
144+
def hamcrestJUnit4TestsProject = project(':hamcrest-junit4-tests')
145+
hamcrestJUnit4Tests(MavenPublication) {
146+
from hamcrestJUnit4TestsProject.components.java
147+
artifactId hamcrestJUnit4TestsProject.name
148+
artifact hamcrestJUnit4TestsProject.sourcesJar
149+
artifact hamcrestJUnit4TestsProject.javadocJar
150+
pom pomConfigurationFor(
151+
'Hamcrest JUnit4 Tests',
152+
'A test suite for Hamcrest assumptions using JUnit 4')
153+
}
154+
155+
def hamcrestJUnit4JUnit5TestsProject = project(':hamcrest-junit4-junit5-tests')
156+
hamcrestJUnit4JUnit5Tests(MavenPublication) {
157+
from hamcrestJUnit4JUnit5TestsProject.components.java
158+
artifactId hamcrestJUnit4JUnit5TestsProject.name
159+
artifact hamcrestJUnit4JUnit5TestsProject.sourcesJar
160+
artifact hamcrestJUnit4JUnit5TestsProject.javadocJar
161+
pom pomConfigurationFor(
162+
'Hamcrest Hybrid JUnit 4/JUnit 5 Tests',
163+
'A test suite for Hamcrest assumptions using hybrid JUnit 4/JUnit 5')
164+
}
132165
}
133166
repositories {
134167
if (publishToOssrh) {
@@ -150,4 +183,7 @@ signing {
150183
sign publishing.publications.hamcrest
151184
sign publishing.publications.hamcrestCore
152185
sign publishing.publications.hamcrestLibrary
186+
sign publishing.publications.hamcrestJUnit5Tests
187+
sign publishing.publications.hamcrestJUnit4Tests
188+
sign publishing.publications.hamcrestJUnit4JUnit5Tests
153189
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
group 'org.hamcrest'
6+
version '2.3-SNAPSHOT'
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
testImplementation project(':hamcrest')
14+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
15+
testImplementation 'org.junit.vintage:junit-vintage-engine:5.8.2'
16+
}
17+
18+
test {
19+
useJUnitPlatform()
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.hamcrest;
2+
3+
import org.junit.Test;
4+
import org.junit.AssumptionViolatedException;
5+
import org.opentest4j.TestAbortedException;
6+
7+
import static org.hamcrest.MatcherAssume.assumeThat;
8+
import static org.hamcrest.Matchers.startsWith;
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.fail;
11+
12+
/**
13+
* Tests compatibility with JUnit 4 <i>and</i> JUnit 5 on the classpath.
14+
* The equivalent test with only JUnit 4 on the classpath is in another module.
15+
*/
16+
public class JUnit4MatcherAssumeTest {
17+
18+
@Test public void
19+
assumptionFailsWithMessage() {
20+
try {
21+
assumeThat("Custom assumption", "a", startsWith("abc"));
22+
fail("should have failed");
23+
}
24+
catch (AssumptionViolatedException e) {
25+
assertEquals("Custom assumption: got: \"a\", expected: a string starting with \"abc\"", e.getMessage());
26+
}
27+
catch (TestAbortedException e) {
28+
throw new AssertionError("Illegal JUnit 5 assumption", e);
29+
}
30+
}
31+
32+
@Test public void
33+
assumptionFailsWithDefaultMessage() {
34+
try {
35+
assumeThat("a", startsWith("abc"));
36+
fail("should have failed");
37+
}
38+
catch (AssumptionViolatedException e) {
39+
assertEquals(": got: \"a\", expected: a string starting with \"abc\"", e.getMessage());
40+
}
41+
catch (TestAbortedException e) {
42+
throw new AssertionError("Illegal JUnit 5 assumption", e);
43+
}
44+
}
45+
46+
@Test public void
47+
assumptionSucceeds() {
48+
try {
49+
assumeThat("xyz", startsWith("xy"));
50+
} catch (TestAbortedException e) {
51+
throw new AssertionError("Illegal JUnit 5 assumption", e);
52+
}
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.hamcrest;
2+
3+
import org.junit.AssumptionViolatedException;
4+
import org.junit.jupiter.api.Test;
5+
import org.opentest4j.TestAbortedException;
6+
7+
import static org.hamcrest.MatcherAssume.assumeThat;
8+
import static org.hamcrest.Matchers.startsWith;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.fail;
11+
12+
/**
13+
* Tests compatibility with JUnit 5 with JUnit 4 <i>and</i> JUnit 5 on the classpath.
14+
* The equivalent test with only JUnit 4 on the classpath is in another module.
15+
*/
16+
class JUnit5MatcherAssumeTest {
17+
18+
@Test
19+
void
20+
assumptionFailsWithMessage() {
21+
try {
22+
assumeThat("Custom assumption", "a", startsWith("abc"));
23+
fail("should have failed");
24+
}
25+
catch (TestAbortedException e) {
26+
assertEquals("Assumption failed: Custom assumption", e.getMessage());
27+
}
28+
catch (AssumptionViolatedException e) {
29+
// If we don't catch JUnit 4 exceptions here, then this test will result in a false positive, or actually
30+
// a false ignored test.
31+
throw new AssertionError("Illegal JUnit 4 assumption", e);
32+
}
33+
}
34+
35+
@Test void
36+
assumptionFailsWithDefaultMessage() {
37+
try {
38+
assumeThat("a", startsWith("abc"));
39+
fail("should have failed");
40+
}
41+
catch (TestAbortedException e) {
42+
assertEquals("Assumption failed", e.getMessage());
43+
}
44+
catch (AssumptionViolatedException e) {
45+
// If we don't catch JUnit 4 exceptions here, then this test will result in a false positive, or actually
46+
// a false ignored test.
47+
throw new AssertionError("Illegal JUnit 4 assumption", e);
48+
}
49+
}
50+
51+
@Test void
52+
assumptionSucceeds() {
53+
try {
54+
assumeThat("xyz", startsWith("xy"));
55+
}
56+
catch (AssumptionViolatedException e) {
57+
// If we don't catch JUnit 4 exceptions here, then this test will result in a false positive, or actually
58+
// a false ignored test.
59+
throw new AssertionError("Illegal JUnit 4 assumption", e);
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.hamcrest;
2+
3+
import org.junit.AssumptionViolatedException;
4+
import org.junit.jupiter.api.Test;
5+
import org.opentest4j.TestAbortedException;
6+
7+
import java.util.concurrent.ExecutionException;
8+
import java.util.concurrent.ExecutorService;
9+
10+
import static java.util.concurrent.Executors.newSingleThreadExecutor;
11+
import static org.hamcrest.MatcherAssume.assumeThat;
12+
import static org.hamcrest.Matchers.is;
13+
import static org.junit.jupiter.api.Assertions.fail;
14+
15+
class MatcherAssumeTest {
16+
17+
@Test
18+
void assumptionFailsWithAssertionErrorWhenNoJUnitInStackTrace() throws Throwable {
19+
// Run the assumption on a separate thread to make sure it has JUnit 4 nor JUnit 5 in its stack trace.
20+
ExecutorService executor = newSingleThreadExecutor();
21+
try {
22+
try {
23+
executor.submit(new Runnable() {
24+
25+
@Override
26+
public void run() {
27+
assumeThat(1, is(2));
28+
}
29+
}).get();
30+
fail("Expected " + ExecutionException.class);
31+
} catch (ExecutionException expected) {
32+
throw expected.getCause();
33+
}
34+
} catch (AssertionError expected) {
35+
} catch (TestAbortedException | AssumptionViolatedException e) {
36+
throw new AssertionError(e);
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
dependencies {
2+
testImplementation project(':hamcrest')
3+
testImplementation(group: 'junit', name: 'junit', version: '4.13.2') {
4+
transitive = false
5+
}
6+
}
7+
8+
jar {
9+
manifest {
10+
attributes 'Implementation-Title': project.name,
11+
'Implementation-Vendor': 'hamcrest.org',
12+
'Implementation-Version': version,
13+
'Automatic-Module-Name': 'org.hamcrest.junit4-tests'
14+
}
15+
}
16+
17+
javadoc.title = "Hamcrest JUnit 4 Tests $version"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.hamcrest;
2+
3+
import org.junit.Test;
4+
import org.junit.AssumptionViolatedException;
5+
6+
import static org.hamcrest.MatcherAssume.assumeThat;
7+
import static org.hamcrest.Matchers.startsWith;
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.fail;
10+
11+
/**
12+
* Tests compatibility with JUnit 4 with only JUnit 4 on the classpath.
13+
* The equivalent test with JUnit 4 <i>and</i> JUnit 5 on the classpath is in another module.
14+
*/
15+
public class JUnit4MatcherAssumeTest {
16+
17+
@Test public void
18+
assumptionFailsWithMessage() {
19+
try {
20+
assumeThat("Custom assumption", "a", startsWith("abc"));
21+
fail("should have failed");
22+
}
23+
catch (AssumptionViolatedException e) {
24+
assertEquals("Custom assumption: got: \"a\", expected: a string starting with \"abc\"", e.getMessage());
25+
}
26+
}
27+
28+
@Test public void
29+
assumptionFailsWithDefaultMessage() {
30+
try {
31+
assumeThat("a", startsWith("abc"));
32+
fail("should have failed");
33+
}
34+
catch (AssumptionViolatedException e) {
35+
assertEquals(": got: \"a\", expected: a string starting with \"abc\"", e.getMessage());
36+
}
37+
}
38+
39+
@Test public void
40+
assumptionSucceeds() {
41+
assumeThat("xyz", startsWith("xy"));
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
dependencies {
2+
api project(':hamcrest')
3+
api 'org.opentest4j:opentest4j:1.2.0'
4+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
5+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
6+
}
7+
8+
jar {
9+
manifest {
10+
attributes 'Implementation-Title': project.name,
11+
'Implementation-Vendor': 'hamcrest.org',
12+
'Implementation-Version': version,
13+
'Automatic-Module-Name': 'org.hamcrest.junit5-tests'
14+
}
15+
}
16+
17+
test {
18+
useJUnitPlatform()
19+
}
20+
21+
javadoc.title = "Hamcrest JUnit 5 Tests $version"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.hamcrest;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.opentest4j.TestAbortedException;
5+
6+
import static org.hamcrest.MatcherAssume.assumeThat;
7+
import static org.hamcrest.Matchers.startsWith;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.fail;
10+
11+
/**
12+
* Tests compatibility with JUnit 5 with only JUnit 5 on the classpath.
13+
* The equivalent test with JUnit 4 <i>and</i> JUnit 5 on the classpath is in another module.
14+
*/
15+
class JUnit5MatcherAssumeTest {
16+
17+
@Test
18+
void
19+
assumptionFailsWithMessage() {
20+
try {
21+
assumeThat("Custom assumption", "a", startsWith("abc"));
22+
fail("should have failed");
23+
}
24+
catch (TestAbortedException e) {
25+
assertEquals("Assumption failed: Custom assumption", e.getMessage());
26+
}
27+
}
28+
29+
@Test void
30+
assumptionFailsWithDefaultMessage() {
31+
try {
32+
assumeThat("a", startsWith("abc"));
33+
fail("should have failed");
34+
}
35+
catch (TestAbortedException e) {
36+
assertEquals("Assumption failed", e.getMessage());
37+
}
38+
}
39+
40+
@Test void
41+
assumptionSucceeds() {
42+
assumeThat("xyz", startsWith("xy"));
43+
}
44+
}

hamcrest/hamcrest.gradle

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ apply plugin: 'osgi'
33
version = rootProject.version
44

55
dependencies {
6-
testImplementation(group: 'junit', name: 'junit', version: '4.13') {
6+
compileOnly 'org.junit.jupiter:junit-jupiter-api:5.8.2'
7+
compileOnly 'junit:junit:4.13.2'
8+
testImplementation(group: 'junit', name: 'junit', version: '4.13.2') {
79
transitive = false
810
}
911
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.hamcrest;
2+
3+
import org.hamcrest.internal.AssumptionProvider;
4+
5+
public final class MatcherAssume {
6+
7+
private MatcherAssume() {
8+
}
9+
10+
public static <T> void assumeThat(T actual, Matcher<? super T> matcher) {
11+
assumeThat("", actual, matcher);
12+
}
13+
14+
public static <T> void assumeThat(String message, T actual, Matcher<? super T> matcher) {
15+
AssumptionProvider.getInstance().assumeThat(message, actual, matcher);
16+
}
17+
}

0 commit comments

Comments
 (0)