Junit5 Assumptions.
Assumptions are used to run tests only if certain conditions are met. This is typically used for external conditions that are required for the test to execute properly, but which are not directly related to whatever is being unit tested.
If the assumeTrue()
condition is true, then run the test, else aborting the test.
If the assumeFalse()
condition is false, then run the test, else aborting the test.
The assumingThat()
is much more flexible, it allows part of the code to run as a conditional test.
When the assumption is false, a TestAbortedException is thrown and the test is aborting execution.
@Test
void trueAssumption() {
assumeTrue(6 > 2);
assertEquals(6 + 2, 8);
}
@Test
void falseAssumption() {
assumeFalse(4 < 1);
assertEquals(4 + 2, 6);
}
@Test
void assumptionThat() {
String str = "a simple string";
assumingThat(
str.equals("a simple string"),
() -> assertEquals(3 + 2, 1)
);
}