Introduction
JUnit is an open-source Regression testing framework for Java. A test in JUnit is a method or class used only for testing. These are called Test methods or Tests classes. To define a method as a test method, you can add @Test annotation before it. There are a few other annotations like @After, @Before, @Ignore, etc., in JUnit.
Assumptions in JUnit are a collection of utility methods that support conditional test execution based on assumptions. Assumptions are used whenever we don’t want to continue the execution of a given test method. It runs the tests only if the conditions are evaluated to be true. As shown below, these assumptions can be imported from the Jupiter API packages.
org.junit.jupiter.api.Assumptions
This statement imports the assumptions into our code. If the assumptions are failed, it results in aborted tests instead of test failure. Let’s learn more about assumptions in this article.
JUnit Assumptions
Assumptions provide us with three methods that can be overloaded.
- Assumptions.assumeTrue()
- Assumptions. false()
- Assumptions.assumingThat()
Assumptions.assumeTrue()
The tests are performed if the conditions are evaluated to be true or aborted. This assumption provides us with the following overloaded methods.
public static void assumeTrue(boolean assumption) throws TestAbortedException
public static void assumeTrue(boolean assumption, Supplier<String> messageSupplier) throws TestAbortedException
public static void assumeTrue(boolean assumption, String message) throws TestAbortedException
public static void assumeTrue(BooleanSupplier assumptionSupplier) throws TestAbortedException
public static void assumeTrue(BooleanSupplier assumptionSupplier, String message) throws TestAbortedException
public static void assumeTrue(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier) throws TestAbortedExceptionAssumptions. false()
The tests are performed if the conditions are evaluated to be false, else they are aborted. This assumption provides us with the following overloaded methods.
public static void assumeFalse(boolean assumption) throws TestAbortedException
public static void assumeFalse(boolean assumption, Supplier<String> messageSupplier) throws TestAbortedException
public static void assumeFalse(boolean assumption, String message) throws TestAbortedException
public static void assumeFalse(BooleanSupplier assumptionSupplier) throws TestAbortedException
public static void assumeFalse(BooleanSupplier assumptionSupplier, String message) throws TestAbortedException
public static void assumeFalse(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier) throws TestAbortedExceptionAssumptions.That()
The tests are performed if the conditions are evaluated to be true. Otherwise, they are not aborted, and the rest of the code in the test is performed. This assumption provides us with the following overloaded methods.
public static void assumingThat​(boolean assumption, Executable executable)
public static void assumingThat​(BooleanSupplier assumptionSupplier, Executable executable)
Let’s learn how to use these assumptions and methods in our code now.
public class JUnit5Assumptions {
@Test
void testOnlyOnDevEnvironment() {
Assumptions.assumeTrue("DEV".equals(System.getenv("ENV_SETUP")));
User user = new User(1, "Peter", "peterm@email.com");
UserService.saveOrUpdate(user);
assertTrue(UserService.users.get(new Long(1)) == user);
}
@Test
void testOnlyIfNotOnProdEnvironment() {
Assumptions.assumeFalse("PROD".equals(System.getenv("ENV_SETUP")));
User user = new User(1, "codingninjas", "codingninjas@email.com");
UserService.saveOrUpdate(user);
assertTrue(UserService.users.get(new Long(1)) == user);
}
@Test
void testOnlyOnDevEnvironment() {
Assumptions.assumeTrue("DEV".equals(System.getenv("ENV_SETUP")),
() -> "Aborting test: not on developer workstation"codingninjas@email.com);
User user = new User(1, "codingninjas", "codingninjas@email.com");
UserService.saveOrUpdate(user);
assertTrue(UserService.users.get(new Long(1)) == user);
}
@Test
void testInAllEnvironments() {
Assumptions.assumingThat("DEV".equals(System.getenv("ENV_SETUP"))
() -> {
UserService.saveOrUpdate(new User(1, "codingninjas", "codingninjas@email.com")); // perform only on dev environment
});
User user = new User(1, "codingninjas", "codingninjas@email.com");
UserService.saveOrUpdate(user);
assertTrue(UserService.users.get(new Long(1)) == user);
}
}
We created a class JUnit5Assumptions in the above code and three methods with each assumption.
- In the method testOnlyIfNotOnProdEnvironment() the assumeFalse() method is used to check if the environment is production or not. If it is true, the test is executed to check if the newly created user and the existing user are the same; otherwise, the test is aborted.
- In the method testOnlyOnDevEnvironment(), the assumeTrue() method is used to check if the environment is a developer workstation or not. If it is true, the test is executed to check if the newly created user and the existing user are the same; otherwise, the test is aborted with a message “Aborting test: not on developer workstation”.
- In the method testInAllEnvironments() the assumeThat() method is used to check all the environments. The updation of the user is performed only in the dev environment, and the assertion check is done on all environments.




