assumeTrue()

This assumeTrue() method is used for checking the condition of the test case. If the assumeTrue() condition is true, then run the given test case otherwise the test will be aborted.

Example:

Java




import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
  
public class AddTwoNumbersTest {
  
    @Test
    void testAddition() {
        int num1 = 2;
        int num2 = 3;
  
        // Assume that the sum of two positive numbers is always greater than zero
        assumeTrue(num1 > 0 && num2 > 0);
  
        // If the assumption is true, the test continues to this point
        int sum = addNumbers(num1, num2);
        assertEquals(5, sum, "Sum should be 5");
    }
  
    private int addNumbers(int a, int b) {
        return a + b;
    }
}


In the code we have used assumeTrue() method for checking the given two number num1 and num2 are greater than 0. If assumeTrue condition is true, then they will be passed. Otherwise, it is aborted. In this case test case will be passed.

JUnit 5 – Assumptions

Assumptions is one the features of JUnit 5, and the JUnit 5 Assumptions are collections unity methods that support conditional-based test case executions of software applications. One more thing Assumptions are used whenever don’t want to continue the execution of given test methods, And the Assumptions run test cases only if the conditions are evaluated to be true. In Assumptions lot of methods are available for different functionalities. Now I will explain available methods in Assumptions of JUnit 5 and mostly these methods assumingThat(), assumeTrue(), and assumeFalse() are used to Validate for given Assumptions and the method type is static void.

  • assumingThat
  • assumeTrue
  • assumeFalse

Similar Reads

1. assumingThat()

The assumingThat() method executes the supplied Executable but only if the supplied Assumption is valid if the given Assumptions is invalid this method does nothing and if the executable throws an exception, it will re-throw the exception as an unchecked exception....

2. assumeTrue()

...

3. assumeFalse()

This assumeTrue() method is used for checking the condition of the test case. If the assumeTrue() condition is true, then run the given test case otherwise the test will be aborted....