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
Reference
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner

Comments
Post a Comment
Leave Comment