From 6e3f583736c638365b9c1bd75eb7897f8cbb6a27 Mon Sep 17 00:00:00 2001 From: Peter De Maeyer Date: Tue, 23 Feb 2021 21:42:02 +0100 Subject: [PATCH] #322 Added MatcherAssume.assumeThat again --- hamcrest/hamcrest.gradle | 1 + .../main/java/org/hamcrest/MatcherAssume.java | 27 +++++++++++++ .../java/org/hamcrest/MatcherAssumeTest.java | 38 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 hamcrest/src/main/java/org/hamcrest/MatcherAssume.java create mode 100644 hamcrest/src/test/java/org/hamcrest/MatcherAssumeTest.java diff --git a/hamcrest/hamcrest.gradle b/hamcrest/hamcrest.gradle index 749b7255..713a8a35 100644 --- a/hamcrest/hamcrest.gradle +++ b/hamcrest/hamcrest.gradle @@ -3,6 +3,7 @@ apply plugin: 'osgi' version = rootProject.version dependencies { + api 'org.opentest4j:opentest4j:1.2.0' testImplementation(group: 'junit', name: 'junit', version: '4.13') { transitive = false } diff --git a/hamcrest/src/main/java/org/hamcrest/MatcherAssume.java b/hamcrest/src/main/java/org/hamcrest/MatcherAssume.java new file mode 100644 index 00000000..96f06822 --- /dev/null +++ b/hamcrest/src/main/java/org/hamcrest/MatcherAssume.java @@ -0,0 +1,27 @@ +package org.hamcrest; + +import org.opentest4j.TestAbortedException; + +public final class MatcherAssume { + + private MatcherAssume() { + } + + public static void assumeThat(T assumption, Matcher matcher) { + assumeThat("", assumption, matcher); + } + + public static void assumeThat(String message, T assumption, Matcher matcher) { + if (!matcher.matches(assumption)) { + throwTestAbortedException(message); + } + } + + private static void throwTestAbortedException(String message) { + throw new TestAbortedException(isNotBlank(message) ? "Assumption failed: " + message : "Assumption failed"); + } + + private static boolean isNotBlank(String string) { + return string != null && !string.trim().isEmpty(); + } +} diff --git a/hamcrest/src/test/java/org/hamcrest/MatcherAssumeTest.java b/hamcrest/src/test/java/org/hamcrest/MatcherAssumeTest.java new file mode 100644 index 00000000..1abe2f93 --- /dev/null +++ b/hamcrest/src/test/java/org/hamcrest/MatcherAssumeTest.java @@ -0,0 +1,38 @@ +package org.hamcrest; + +import org.junit.Test; +import org.opentest4j.TestAbortedException; + +import static org.hamcrest.MatcherAssume.assumeThat; +import static org.hamcrest.Matchers.startsWith; +import static org.junit.Assert.*; + +public class MatcherAssumeTest { + + @Test public void + assumptionFailsWithMessage() { + try { + assumeThat("Custom assumption", "a", startsWith("abc")); + fail("should have failed"); + } + catch (TestAbortedException e) { + assertEquals("Assumption failed: Custom assumption", e.getMessage()); + } + } + + @Test public void + assumptionFailsWithDefaultMessage() { + try { + assumeThat("a", startsWith("abc")); + fail("should have failed"); + } + catch (TestAbortedException e) { + assertEquals("Assumption failed", e.getMessage()); + } + } + + @Test public void + assumptionSucceeds() { + assumeThat("xyz", startsWith("xy")); + } +} \ No newline at end of file