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.
JUnit jupiter Assumptions
class has two such methods: assumeFalse()
, assumeTrue()
.
Experimental
state and might be confirmed in future.Table of Contents Assumptions.assumeTrue() Assumptions.assumeFalse()
JUnit 5 Assumptions.assumeTrue()
assumeTrue()
validates the given assumption to true and if assumption is true – test proceed, otherwise test execution is aborted.
It has 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 :: "; } }
JUnit 5 Assumptions.assumeFalse()
assumeFalse()
validates the given assumption to false and if assumption is false – test proceed, otherwise test execution is aborted. It works just opposite to assumeTrue()
.
It has 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 :: "; } }
Drop me your questions in comments section.
Happy Learning !!
kumar
So, What’s the difference b/w Assumptions and Assertions?
lolo101
A failed *assumptions* makes the test pass
A failed *assertion* makes the test fail
Himanshu Mittal
A failed assumption means you don’t run the test because something you assumed to be true is not true and so you just skip the test.
https://stackoverflow.com/questions/44628483/assume-vs-assert-in-junit-tests
Asgher Ali
Good article, thanks for the information. Also it would have been useful if parameterized tests was also discussed.