JUnit Assumptions - assumingThat Example

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

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

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare