Junit-5 org.junit.jupiter.api.Assumptions class has static methods to support conditional test execution based on assumptions.
If the assumption is failed then the test will be aborted. Assumptions are typically used whenever it does not make sense to continue the execution of a given test method.
JUnit Jupiter org.junit.jupiter.api.Assumptions class has three such methods: assumeFalse(), assumeTrue() and assumingThat()
Assumptions.assumeTrue()
assumeTrue() assumption validates the given assumption to true and if assumption is true – test proceed otherwise test execution is aborted.
Assumptions.assumeFalse()
assumeFalse() assumption validates the given assumption to false and if assumption is false test proceed, otherwise test execution is aborted. It’s functionality is just opposite to assumeTrue().
Assumptions.assumingThat()
This assumption method executes the supplied Executable
, but only if the supplied assumption is valid. If this assumption is invalid then this method does nothing.
If the executable
throws an exception, it will be rethrown as is but as an unchecked exception.
NOTE: org.junit.jupiter.api.Assumptions class has almost 14 methods and most of them are an overloaded version of assumeFalse(), assumeTrue(), and assumingThat() methods
Let’s try to understand assumptions using a demo project.
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>JUnit5AssumptionsExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.6.2</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
MyUtils.java
1 2 3 4 5 6 7 8 |
package com.kkjavatutorials; public class MyUtils { public int add(int n1, int n2) { return n1+n2; } } |
Test class MyUtilsTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assumptions.assumeFalse; import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.junit.jupiter.api.Assumptions.assumingThat; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @DisplayName(value = "All the Test Cases of MyUtilsTest") public class MyUtilsTest { private static MyUtils myUtils; @BeforeAll static void setUp() { myUtils = new MyUtils(); System.out.println("Test data set up is done.."); } @BeforeEach void beforeEach() { System.out.println("@BeforeEach is executed.."); } @Test @DisplayName(value = "adding two positive numbers") void test_add_two_positive_numbers() { System.setProperty("ENV", "DEV"); assumeTrue("DEV".equals(System.getProperty("ENV"))); //If above assumption is true then rest of test will proceed int actualResult = myUtils.add(20, 10); assertEquals(30, actualResult); } @Test @DisplayName(value = "adding two negative numbers") void test_add_two_negative_numbers() { System.setProperty("ENV", "PROD"); assumeTrue("DEV".equals(System.getProperty("ENV")), MyUtilsTest::message); int actualResult = myUtils.add(-20, -10); assertEquals(-30, actualResult); } @Test @DisplayName(value = "adding one positive and one negative number") void test_add_one_positive_and_one_negative_number() { System.setProperty("ENV", "DEV"); assumeFalse("DEV".equals(System.getProperty("ENV")), MyUtilsTest::message); //If above assumption is false then rest of test will be aborted int actualResult = myUtils.add(20, -10); assertEquals(10, actualResult); } @Test @DisplayName(value = "adding one two numbers") void testInAllEnvironments() { System.setProperty("ENV", "CI"); assumingThat("CI".equals(System.getenv("ENV")), () -> { // This assertion only perform on the CI server assertEquals(60, myUtils.add(40, 20)); }); // perform these assertions in all environments assertEquals(130, myUtils.add(60, 70)); } @AfterEach void afterEach() { System.out.println("@AfterEach is executed.."); } @AfterAll static void tearDown() { myUtils = null; System.out.println("Test data teardown is done.."); } private static String message () { return "Test Execution is Failed. "; } } |
The output of the above project:
You May Also Like:
Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
That’s all about JUnit 5 Assumptions Examples
If you have any feedback or suggestion please feel free to drop in below comment box.