In Junit 5 Assumptions are collection of utility methods that support conditional test execution based on assumptions. Failed assumptions do not result in a test failure; rather, a failed assumption results in a test being aborted.
Assumption basically means “don’t run this test if these conditions don’t apply”.
Junit 5 comes with a subset of the assumption methods that JUnit 4 provides with Java 8 lambda expressions and method references. All JUnit Jupiter assumptions are static methods in the org.junit.jupiter.api.Assumptions
class.
1. Assumptions class in Junit 5 :
Assumptions
class provides following overloaded methods.
Assumptions.assumeTrue()
– If the condition is true, then run the test, else aborting the test.Assumptions.false()
– If the condition is false, then run the test, else aborting the test.Assumptions.assumingThat()
– is much more flexible, If condition is true then executes, else do not abort test continue rest of code in test.
Lets have a look into test cases using Assumptions class examples in following application.
2. Assumptions Examples
2.1. Technologies used :
- Junit 5.5.2
- Maven 3
- Java 8
- Spring Tool Suite 3.9.8
2.2 Example project structure :
2.3. Example
public class JUnit5_Assumptions_Test { // assumeTrue() -> Run only if environment is DEV @Test void testOnlyOnDevEnvironment() { assumeTrue("DEV".equals(System.getenv("ENV_SETUP"))); User user = new User(1, "Peter", "[email protected]"); UserService.saveOrUpdate(user); assertTrue(UserService.users.get(new Long(1)) == user); } // assumeFalse() -> Run only if environment is not PROD @Test void testOnlyIfNotOnProdEnvironment() { assumeFalse("PROD".equals(System.getenv("ENV_SETUP"))); User user = new User(1, "Peter", "[email protected]"); UserService.saveOrUpdate(user); assertTrue(UserService.users.get(new Long(1)) == user); } // assumeTrue() -> Run only if environment is DEV, if not display message @Test void testOnlyOnDeveloperWorkstation() { assumeTrue("DEV".equals(System.getenv("ENV_SETUP")), () -> "Aborting test: not on developer workstation"); // not perform if assuption fail User user = new User(1, "Peter", "[email protected]"); UserService.saveOrUpdate(user); assertTrue(UserService.users.get(new Long(1)) == user); } // assumeThat() @Test void testInAllEnvironments() { assumingThat("DEV".equals(System.getenv("ENV_SETUP")), () -> { // perform these assertions only on the DEV server UserService.saveOrUpdate(new User(1, "Peter", "[email protected]")); }); // perform these assertions in all environments User user = new User(1, "Peter", "[email protected]"); UserService.saveOrUpdate(user); assertTrue(UserService.users.get(new Long(1)) == user); } }
Run example test case using eclipse.
2.4. Test results :
Conclusion :
In this article we have seen, what are Assumptions in Junit 5, different methods in org.junit.jupiter.api.Assumptions
class with a simple example application. As of JUnit Jupiter 5.4, it is also possible to use methods from JUnit 4’s org.junit.Assume
class for assumptions. You also might be interested in Junit 5 Conditional Tests.
You also might be interested in following examples :
- Junit 5 Dynamic Tests and @TestFactory annotation.
- Junit 5 tags and filter test cases for execution.
- Junit 5 Timeout tests, fail tests if not completed within time.
- Junit 5 Repeated tests and display Repetition info.
- Junit 5 conditional tests execution examples
- Junit 5 Disable tests using @Disabled annotation