JUnit 5 Assumptions with Examples

In this article, we will discuss the different methods being offered by the JUnit5 Assumptions class, how we can use them, and what is the difference between Assumptions and Assertions.


1. Why Assumptions?

Assumptions.class in JUnit5 contains a collection of methods that enables us to programmatically decide whether to continue executing the test or skip the further execution.

Currently Assumptions class contains overloaded versions of three different methods.

  • assumeTrue
  • assumeFalse
  • assumingThat

Note: All of these methods are static and have multiple overloaded versions. Reference of class Assumptions

Let’s discuss each of them in details.


2. assumeTrue

If the condition in assumeTrue is true then only the rest of the test method is executed, else the test is skipped.

Let’s implement a test using assumeTrue method.

@Test
void testAssumeTrueOnTestEnv() {
  System.setProperty("ENV", "TEST");

  assumeTrue("TEST".equals(System.getProperty("ENV")));

  // Since the condition is true rest of it will get executed
  assertTrue(Math.random() > 0);
}

3. assumeFalse

If the condition in assumeFalse is false then only the rest of the test method is executed, else the test is skipped.

Let’s implement a test using assumeFalse method.

@Test
void testAssumeFalseWithFalseCondition() {
  System.setProperty("ENV", "PRODUCTION");

  assumeFalse("TEST".equals(System.getProperty("ENV")));

  // Since the condition is false rest of it will get executed
  assertTrue(Math.random() > 0);
}

4. assumingThat

assumingThat runs the executable only if the condition is true.

This method is a bit different from the other two that we have discussed about because if the condition is false, then it just skips the executable block and the rest of the test method is still executed.

Let’s implement a test using assumingThat method with the true condition so that the executable executes.

@Test
void testAssumingThat() {
  System.setProperty("ENV", "CI");

  assumingThat(
      "CI".equals(System.getProperty("ENV")),
      () -> {
        // run the end2end test cases
        System.out.println("Assuming that executable executed");
      });

  // Since the condition is false rest of it will get executed
  assertTrue(Math.random() > 0);
}

5. Complete Assumptions Example

Let’s implement the whole example with all the assumption methods and their output.

package com.codingeek;

import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.*;

@DisplayNameGeneration(DisplayNameTest.CamelCaseToStatementGenerator.class)
class AssumptionsTest {
  @Test
  void testAssumeTrueOnTestEnv() {
    System.setProperty("ENV", "TEST");

    assumeTrue("TEST".equals(System.getProperty("ENV")));

    // Since the condition is true rest of it will get executed
    assertTrue(Math.random() > 0);
  }

  @Test
  void testAssumeTrueOnTestEnvWithFalseConditionWithMessageSupplier() {
    System.setProperty("ENV", "TEST");

    assumeTrue("PRODUCTION".equals(System.getProperty("ENV")), AssumptionsTest::message);

    // Since the condition is false rest of it will NOT get executed
    assertTrue(Math.random() > 0);
  }

  @Test
  void testAssumeFalseWithFalseCondition() {
    System.setProperty("ENV", "PRODUCTION");

    assumeFalse("TEST".equals(System.getProperty("ENV")));

    // Since the condition is false rest of it will get executed
    assertTrue(Math.random() > 0);
  }

  @Test
  void testAssumeFalseWithTrueConditionWithMessageSupplier() {
    System.setProperty("ENV", "PRODUCTION");

    assumeFalse("PRODUCTION".equals(System.getProperty("ENV")), AssumptionsTest::message);

    // Since the condition is false rest of it will get executed
    assertTrue(Math.random() > 0);
  }

  @Test
  void testAssumingThat() {
    System.setProperty("ENV", "CI");

    assumingThat(
        "CI".equals(System.getProperty("ENV")),
        () -> {
          // run the end2end test cases
          System.out.println("Assuming that executable executed");
        });

    // Since the condition is false rest of it will get executed
    assertTrue(Math.random() > 0);
  }

  private static String message() {
    return "TEST Execution Failed :: ";
  }
}
Output:-
> Task :app:test
Assumptions test [ com.codingeek.AssumptionsTest ]
    ಠ_ಠ  test assume true on test env with false condition with message supplier()
    ✓  test assume false with false condition()
    ಠ_ಠ  test assume false with true condition with message supplier()
    ✓  test assume true on test env()
    ✓  test assuming that()

--------------------------------------------------------------------------
Results: SUCCESS (5 tests, 3 passed, 0 failed, 2 skipped)
--------------------------------------------------------------------------

 Skipped Tests:
        com.codingeek.AssumptionsTest - testAssumeTrueOnTestEnvWithFalseConditionWithMessageSupplier()
        com.codingeek.AssumptionsTest - testAssumeFalseWithTrueConditionWithMessageSupplier()

Read More: How to create custom Junit display name generator?

6. Difference between Assumption & Assertion

The main difference between the assertions and assumptions is –

The assumption is used to decide whether we want to execute a section or the rest of the test method or not and if the condition is false then the test is skipped.

Whereas if a condition in an assertion fails then it fails the test and something needs to be fixed.

Read More: A complete JUnit Assertions guide

Complete code samples are present on Github project.


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index