Open In App

JUnit 5 – Assumptions

Last Updated : 03 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

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.

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.

2. 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.

3. assumeFalse()

The assumeFalse() method is used for checking the assumeFalse condition. If the assumeFalse() condition is false then test case will be run, if assumeFalse condition is true then the test case 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.assumeFalse;
  
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
        assumeFalse(num1 > 100 && num2 > 100);
  
        // 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 this above example, the given test is failed means aborted because the given assumeFalse condition is false that’s why this test case will be aborted.

Conclusion

In JUnit 5 the Assumptions are mainly used for check whether the given condition is true or not. If the given condition is true, then Test case execution will be continued otherwise test case is aborted. In Assumptions we have three different methods used for different purposes I already explained in the above. For better understanding you need basic knowledge on JUnit 4 as well as knowledge on Testing Annotations.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads