JUnit 5 Assumptions

JUnit 5 Assumptions class provides static methods to support conditional test execution based on assumptions. A failed assumption results in a test being aborted. Assumptions are typically used whenever it does not make sense to continue execution of a given test method. In test report, these test will be marked as passed.

junit5 logo

Assumptions are typically used whenever it does not make sense to continue the execution of a given test method, for example, if the test depends on something that does not exist in the current runtime environment. In the test report, these tests will be marked as aborted.

JUnit 5 Assumptions class provides static methods to support conditional test execution based on assumptions. A failed assumption results in a test being aborted.

JUnit Jupiter org.junit.jupiter.apiAssumptions class has the following overloaded methods:

  • abort()
  • assumeFalse()
  • assumeTrue()
  • assumingThat()

1. Assumptions.abort()

The abort() method aborts the test with or without a message. Although aborting with an explicit message is recommended, this may be useful when maintaining legacy code.

public class TestClass { @Test void myTest() { Stream.of("...").map(entry -> {	//check assumption	Assumptions.abort("assumption not met") }); } }

2. Assumptions.assumeTrue()

The assumeTrue() method validates the given assumption to be true and if the assumption is true – the test proceeds, otherwise, test execution is aborted.

It has the following overloaded methods.

public static void assumeTrue(boolean assumption) throws TestAbortedException public static void assumeTrue(boolean assumption, Supplier<String> messageSupplier) throws TestAbortedException public static void assumeTrue(boolean assumption, String message) throws TestAbortedException public static void assumeTrue(BooleanSupplier assumptionSupplier) throws TestAbortedException public static void assumeTrue(BooleanSupplier assumptionSupplier, String message) throws TestAbortedException public static void assumeTrue(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier) throws TestAbortedException
public class AppTest {	@Test	void testOnDev() {	System.setProperty("ENV", "DEV");	Assumptions.assumeTrue("DEV".equals(System.getProperty("ENV")));	//remainder of test will proceed	}	@Test	void testOnProd(){	System.setProperty("ENV", "PROD");	Assumptions.assumeTrue("DEV".equals(System.getProperty("ENV")), AppTest::message);	//remainder of test will be aborted	}	private static String message () {	return "TEST Execution Failed :: ";	} }

3. Assumptions.assumeFalse()

The assumeFalse() method validates the given assumption to false and if the assumption is false – test proceed, otherwise, test execution is aborted.

It works just opposite to assumeTrue().

It has the following overloaded methods.

public static void assumeFalse(boolean assumption) throws TestAbortedException public static void assumeFalse(boolean assumption, Supplier<String> messageSupplier) throws TestAbortedException public static void assumeFalse(boolean assumption, String message) throws TestAbortedException public static void assumeFalse(BooleanSupplier assumptionSupplier) throws TestAbortedException public static void assumeFalse(BooleanSupplier assumptionSupplier, String message) throws TestAbortedException public static void assumeFalse(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier) throws TestAbortedException
public class AppTest {	@Test	void testOnDev() {	System.setProperty("ENV", "DEV");	Assumptions.assumeFalse("DEV".equals(System.getProperty("ENV")), AppTest::message);	//remainder of test will be aborted	}	@Test	void testOnProd() {	System.setProperty("ENV", "PROD");	Assumptions.assumeFalse("DEV".equals(System.getProperty("ENV")));	//remainder of test will proceed	}	private static String message () {	return "TEST Execution Failed :: ";	} }

4. Assertions.assumingThat()

This method executes the supplied Executable, but only if the supplied assumption is valid.

Unlike the other assumption methods, this method will not abort the test.

  • If the assumption is invalid, this method does nothing.
  • If the assumption is valid and the executable throws an exception, it will be treated like a regular test failure. The thrown exception will be rethrown as is but masked as an unchecked exception.

It has the following overloaded methods.

public static void assumingThat​(boolean assumption, Executable executable) public static void assumingThat​(BooleanSupplier assumptionSupplier, Executable executable)
@Test void testInAllEnvironments() { assumingThat("DEV".equals(System.getenv("ENV")), () -> { // perform these assertions only on the DEV server assertEquals(2, calculator.divide(4, 2)); }); // perform these assertions in all environments assertEquals(42, calculator.multiply(6, 7)); }

Happy Learning !!

Comments

Subscribe
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.