Assumptions

In this part of this section is about the so-called assumptions. Assumptions allow us to only run tests if certain conditions are as expected. All JUnit Jupiter assumptions are static methods in the class Assumptions, located inside the org.junit.jupiter package. The following screenshot shows all the methods of this class:

Methods of the class org.junit.jupiter.Assumptions

On the one hand, the methods assumeTrue and assumeFalse can be used to skip tests whose preconditions are not met. On the other hand, the method assumingThat is used to condition the execution of a part in a test:

package io.github.bonigarcia;

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

import org.junit.jupiter.api.Test;

class AssumptionsTest {

@Test
void assumeTrueTest() {
assumeTrue(false);
fail("Test 1 failed");
}

@Test
void assumeFalseTest() {
assumeFalse(this::getTrue);
fail("Test 2 failed");
}

private boolean getTrue() {
return true;
}

@Test
void assummingThatTest() {
assumingThat(false, () -> fail("Test 3 failed"));
}

}

Notice that in this example, the two first tests (assumeTrueTest and assumeFalseTest) are skipped since the assumptions are not met. Nevertheless, in the assummingThatTest test, only this part of the test (a lambda expression in this case) is not executed, but the whole test is not skipped:

Execution of assumptions test example
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.114.85