In this article, we will discuss the Assumptions assumingThat() method with an example.
The assumingThat() method executes the supplied Executable if the assumption is valid. If the assumption is invalid, this method does nothing. We can use this for logging or notifications when our assumptions are valid.JUnit Assumptions - assumingThat Example
Assumptions class provides many overloaded assumingThat() methods and the below Java program demonstrates the same.
package junit5.assumptions.assumingThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.junit.jupiter.api.Assumptions.assumingThat;
import org.junit.jupiter.api.Test;
public class AssumingThatDemo {
@Test
public void assumingThatWithBooleanCondition() {
assumingThat("DEV".equals(System.getProperty("ENV")), () -> {
System.out.println("Dev environment !!!");
assertEquals(5, 3 + 2);
});
// below code gets executed in every environment
System.out.println("Executed in every environment !!!");
assertEquals(3, 2 + 1);
}
@Test
public void assumingThatWithBooleanSupplier() {
assumingThat(() -> "DEV".equals(System.getProperty("ENV")), () -> {
System.out.println("Dev environment !!!");
assertEquals(5, 3 + 2);
});
// below code gets executed in every environment
System.out.println("Executed in every environment !!!");
assertEquals(3, 2 + 1);
}
}
Run JUnit test Class
Run the JUnit test class to execute all the JUnit test cases and here is the output:
Comments
Post a Comment
Leave Comment