Skip to content

Commit

Permalink
Document and polish Assumptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Nov 20, 2015
1 parent c868d46 commit baae8bd
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions junit5-api/src/main/java/org/junit/gen5/api/Assumptions.java
Expand Up @@ -12,11 +12,16 @@


import java.util.function.Supplier; import java.util.function.Supplier;


import org.junit.gen5.commons.util.StringUtils;
import org.opentestalliance.TestAbortedException; import org.opentestalliance.TestAbortedException;


/** /**
* Collection of utility methods for aborting test execution based on failed
* assumptions.
*
* @author Sam Brannen * @author Sam Brannen
* @since 5.0 * @since 5.0
* @see TestAbortedException
*/ */
public final class Assumptions { public final class Assumptions {


Expand All @@ -26,37 +31,37 @@ private Assumptions() {


public static void assumeTrue(boolean condition) { public static void assumeTrue(boolean condition) {
if (!condition) { if (!condition) {
throw new TestAbortedException("Assumption failed: condition is not true"); throwTestAbortedException("condition is not true");
} }
} }


public static void assumeTrue(boolean condition, String message) { public static void assumeTrue(boolean condition, String message) {
if (!condition) { if (!condition) {
throw new TestAbortedException("Assumption failed: " + message); throwTestAbortedException(message);
} }
} }


public static void assumeTrue(boolean condition, Supplier<String> messageSupplier) { public static void assumeTrue(boolean condition, Supplier<String> messageSupplier) {
if (!condition) { if (!condition) {
throw new TestAbortedException(messageSupplier.get()); throwTestAbortedException(messageSupplier.get());
} }
} }


public static void assumeFalse(boolean condition) { public static void assumeFalse(boolean condition) {
if (condition) { if (condition) {
throw new TestAbortedException("Assumption failed: condition is not false"); throwTestAbortedException("condition is not false");
} }
} }


public static void assumeFalse(boolean condition, String message) { public static void assumeFalse(boolean condition, String message) {
if (condition) { if (condition) {
throw new TestAbortedException(message); throwTestAbortedException(message);
} }
} }


public static void assumeFalse(boolean condition, Supplier<String> messageSupplier) { public static void assumeFalse(boolean condition, Supplier<String> messageSupplier) {
if (condition) { if (condition) {
throw new TestAbortedException("Assumption failed: " + messageSupplier.get()); throwTestAbortedException(messageSupplier.get());
} }
} }


Expand All @@ -65,15 +70,19 @@ public static void assumingThat(boolean condition, Executable executable) {
try { try {
executable.execute(); executable.execute();
} }
catch (AssertionError | RuntimeException e) { catch (Error | RuntimeException ex) {
// rethrow // rethrow
throw e; throw ex;
} }
catch (Throwable e) { catch (Throwable ex) {
// TODO Don't wrap Throwables such as OutOfMemoryError, etc. throw new RuntimeException("Wrapped checked exception thrown from Executable", ex);
throw new RuntimeException("Wrapped exception thrown from Executable", e);
} }
} }
} }


private static void throwTestAbortedException(String message) {
throw new TestAbortedException(
StringUtils.isNotBlank(message) ? ("Assumption failed: " + message) : "Assumption failed");
}

} }

0 comments on commit baae8bd

Please sign in to comment.