JUnit Assumptions - assumeTrue Example

In this article, we will discuss the Assumptions assumeTrue() method with an example.

The assumeTrue() assumption validates the given assumption to be true and if the assumption is true – the test proceed otherwise test execution is aborted.

JUnit Assumptions - assumeTrue Examples

Assumptions class provides many overloaded assumeTrue() methods, Let's see a few of them with an example.

assumeTrue(boolean assumption)

Validate the given assumption.
	@Test
	public void assumeTrueWithNoMessage() {
		assumeTrue("DEV".equals(System.getProperty("ENV")));
		
		System.out.println("Assumption passed !!!");
		assertEquals(3, 2 + 1);
	}

assumeTrue(boolean assumption, String message)

Validate the given assumption with a message.
	@Test
	public void assumeTrueWithMessage() {
		assumeTrue("DEV".equals(System.getProperty("ENV")), "Assumption Failed !!!");
		
		System.out.println("Assumption passed !!!");
		assertEquals(3, 2 + 1);
	}

assumeTrue(BooleanSupplier assumptionSupplier)

Validate the given assumption with Supplier implementation.
	@Test
	public void assumeTrueWithMessageSupplier() {
		assumeTrue("DEV".equals(System.getProperty("ENV")), () -> "Assumption Failed !!!");
		
		System.out.println("Assumption passed !!!");
		assertEquals(3, 2 + 1);
	}

assumeTrue(boolean assumption, Supplier<String> messageSupplier)

	@Test
	public void assumeTrueWithBooleanSupplierAndMessage() {
		assumeTrue(() -> "DEV".equals(System.getProperty("ENV")), "Assumption Failed !!!");
		
		System.out.println("Assumption passed !!!");
		assertEquals(3, 2 + 1);
	}

Complete JUnit Class

package junit5.assumptions.assumeTrue;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import org.junit.jupiter.api.Test;

public class AssumeTrueDemo {
	
	@Test
	public void assumeTrueWithNoMessage() {
		assumeTrue("DEV".equals(System.getProperty("ENV")));
		
		System.out.println("Assumption passed !!!");
		assertEquals(3, 2 + 1);
	}
	
	@Test
	public void assumeTrueWithMessage() {
		assumeTrue("DEV".equals(System.getProperty("ENV")), "Assumption Failed !!!");
		
		System.out.println("Assumption passed !!!");
		assertEquals(3, 2 + 1);
	}
	
	@Test
	public void assumeTrueWithMessageSupplier() {
		assumeTrue("DEV".equals(System.getProperty("ENV")), () -> "Assumption Failed !!!");
		
		System.out.println("Assumption passed !!!");
		assertEquals(3, 2 + 1);
	}
	
	@Test
	public void assumeTrueWithBooleanSupplierAndNoMessage() {
		assumeTrue(() -> "DEV".equals(System.getProperty("ENV")));
		
		System.out.println("Assumption passed !!!");
		assertEquals(3, 2 + 1);
	}
	
	@Test
	public void assumeTrueWithBooleanSupplierAndMessage() {
		assumeTrue(() -> "DEV".equals(System.getProperty("ENV")), "Assumption Failed !!!");
		
		System.out.println("Assumption passed !!!");
		assertEquals(3, 2 + 1);
	}
	
	@Test
	public void assumeTrueWithBooleanSupplierAndMessageSupplier() {
		assumeTrue(() -> "DEV".equals(System.getProperty("ENV")), () -> "Assumption Failed !!!");
		
		System.out.println("Assumption passed !!!");
		assertEquals(3, 2 + 1);
	}
	
}

Comments