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.

Example:

Java




import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumingThat;
  
public class AddTwoNumbersTest {
  
    @Test
    void testAddition() {
        int num1 = 5;
        int num2 = 7;
        assumingThat(num1 > 0 && num2 > 0, () -> {
            int sum = addNumbers(num1, num2);
            assertEquals(12, sum, "Sum should be 12");
        });
    }
  
    private int addNumbers(int a, int b) {
        return a + b;
    }
}


In the above code, @Test is used for starting the test case and in this example try to add two numbers by using assumingThat() method. The assumingThat() method will check that given num1 and num2 are grater then 0 if it is true then test case will be run otherwise the case execution is aborted. But in this the test case is 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....

Contact Us