Assumptions is a collection of utility methods that support conditional test execution based on assumptions. Failed assumptions do not result in a test failure; rather, a failed assumption results in a test being aborted.

Assumption basically means “don’t run this test if these conditions don’t apply”.

Junit 5 comes with a subset of the assumption methods that JUnit 4 provides with Java 8 lambda expressions and method references. All JUnit Jupiter assumptions are static methods in the org.junit.jupiter.api.Assumptions class.

1. Junit 5 Assumptions:

  1. Assumptions.assumeTrue() –  If the  condition is true, then run the test, else aborting the test.
  2. Assumptions.false() –  If the  condition is false, then run the test, else aborting the test.
  3. Assumptions.assumingThat() –  If condition is true then executes, else do not abort test continue rest of code in test.

Lets have a look into Juint 5 Assumptions in below example.

Technologies used :

  1. Junit 5.5.2
  2. Maven 3
  3. Java 8
  4. Spring Tool Suite 3.9.8

2. JUnit5_Assumptions_Test.java

Junit 5 Assumptions demo project structure.

Junit-5-assumptions-example

Copy to Clipboard

Run with Junit 5 Assumptions example test case using eclipse.

Test results:

Junit 5 assumptions test results