Skip to content

Commit 6e3f583

Browse files
committed
#322 Added MatcherAssume.assumeThat again
1 parent 8522353 commit 6e3f583

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

hamcrest/hamcrest.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ apply plugin: 'osgi'
33
version = rootProject.version
44

55
dependencies {
6+
api 'org.opentest4j:opentest4j:1.2.0'
67
testImplementation(group: 'junit', name: 'junit', version: '4.13') {
78
transitive = false
89
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.hamcrest;
2+
3+
import org.opentest4j.TestAbortedException;
4+
5+
public final class MatcherAssume {
6+
7+
private MatcherAssume() {
8+
}
9+
10+
public static <T> void assumeThat(T assumption, Matcher<? super T> matcher) {
11+
assumeThat("", assumption, matcher);
12+
}
13+
14+
public static <T> void assumeThat(String message, T assumption, Matcher<? super T> matcher) {
15+
if (!matcher.matches(assumption)) {
16+
throwTestAbortedException(message);
17+
}
18+
}
19+
20+
private static void throwTestAbortedException(String message) {
21+
throw new TestAbortedException(isNotBlank(message) ? "Assumption failed: " + message : "Assumption failed");
22+
}
23+
24+
private static boolean isNotBlank(String string) {
25+
return string != null && !string.trim().isEmpty();
26+
}
27+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.hamcrest;
2+
3+
import org.junit.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.Assert.*;
9+
10+
public class MatcherAssumeTest {
11+
12+
@Test public void
13+
assumptionFailsWithMessage() {
14+
try {
15+
assumeThat("Custom assumption", "a", startsWith("abc"));
16+
fail("should have failed");
17+
}
18+
catch (TestAbortedException e) {
19+
assertEquals("Assumption failed: Custom assumption", e.getMessage());
20+
}
21+
}
22+
23+
@Test public void
24+
assumptionFailsWithDefaultMessage() {
25+
try {
26+
assumeThat("a", startsWith("abc"));
27+
fail("should have failed");
28+
}
29+
catch (TestAbortedException e) {
30+
assertEquals("Assumption failed", e.getMessage());
31+
}
32+
}
33+
34+
@Test public void
35+
assumptionSucceeds() {
36+
assumeThat("xyz", startsWith("xy"));
37+
}
38+
}

0 commit comments

Comments
 (0)