JUnit 5 Assumptions Examples

By | October 2, 2020

Junit-5 org.junit.jupiter.api.Assumptions class has static methods to support conditional test execution based on assumptions.

If the assumption is failed then the test will be aborted. Assumptions are typically used whenever it does not make sense to continue the execution of a given test method.

JUnit Jupiter org.junit.jupiter.api.Assumptions  class has three such methods: assumeFalse(), assumeTrue() and assumingThat()

Assumptions.assumeTrue()

assumeTrue()  assumption validates the given assumption to true and if assumption is true – test proceed otherwise test execution is aborted.

Assumptions.assumeFalse()

assumeFalse() assumption validates the given assumption to false and if assumption is false test proceed, otherwise test execution is aborted. It’s functionality is just opposite to assumeTrue().

Assumptions.assumingThat()

This assumption method executes the supplied Executable, but only if the supplied assumption is valid. If this assumption is invalid then this method does nothing.

If the executable throws an exception, it will be rethrown as is but as an unchecked exception.

NOTE: org.junit.jupiter.api.Assumptions class has almost 14 methods and most of them are an overloaded version of  assumeFalse(), assumeTrue(), and assumingThat() methods

Let’s try to understand assumptions using a demo project.

pom.xml

MyUtils.java

Test class MyUtilsTest.java  

The output of the above project: 

You May Also Like:

Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example

That’s all about JUnit 5 Assumptions Examples
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *