JUnit 5 Assumptions Examples

JUnit 5 assumptions

This article shows you how to use JUnit 5 Assumptions to perform a conditional test execution.

Technologies used:

  • Maven 3.6
  • Java 8
  • JUnit 5.5.2

1. Assumptions

1.1 If the assumeTrue() condition is true, then run the test, else aborting the test.

1.2 The assumingThat() is much more flexible, it allows part of the code to run as a conditional test.


package com.mkyong;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.junit.jupiter.api.Assumptions.assumingThat;

public class AssumptionsTest {

    // Output: org.opentest4j.TestAbortedException: Assumption failed: assumption is not true
    @DisplayName("Run this if `assumeTrue` condition is true, else aborting this test")
    void testOnlyOnDevEnvElseAbort() {
        assertEquals(2, 1 + 1);

    // Output: org.opentest4j.TestAbortedException: Assumption failed: Aborting test: not on developer environment
    @DisplayName("Run this if `assumeTrue` condition is true, else aborting this test (Custom Message)")
    void testOnlyOnDevEnvElseAbortWithCustomMsg() {
        assumeTrue("DEV".equals(System.getenv("APP_MODE")), () -> "Aborting test: not on developer environment");
        assertEquals(2, 1 + 1);

    void testAssumingThat() {

        // run these assertions always, just like normal test
        assertEquals(2, 1 + 1);

                () -> {
                    // run this only if assumingThat condition is true
                    assertEquals(2, 1 + 1);

        // run these assertions always, just like normal test
        assertEquals(2, 1 + 1);




ide output

Download Source Code

$ git clone https://github.com/mkyong/junit-examples
$ cd junit5-examples
$ check src/test/java/com/mkyong/AssumptionsTest.java


About the Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.