Table of contents
1.
Introduction
2.
JUnit Assumptions
2.1.
Assumptions.assumeTrue()
2.2.
Assumptions. false()
2.3.
Assumptions.That()
3.
FAQs
4.
Key Takeaways
Last Updated: Jul 11, 2024
Easy

Junit5 Assumptions

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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
You can also try this code with Online Java Compiler
Run Code


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.

  1. Assumptions.assumeTrue()
  2. Assumptions. false() 
  3. 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 TestAbortedException
You can also try this code with Online Java Compiler
Run Code

Assumptions. 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 TestAbortedException
You can also try this code with Online Java Compiler
Run Code

Assumptions.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)
You can also try this code with Online Java Compiler
Run Code


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);
   }
}
You can also try this code with Online Java Compiler
Run Code

 

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.

FAQs

  1. What is JUnit testing?
    JUnit is an open-source Regression testing framework for Java. We can create test cases for our code using the JUnit framework.
     
  2. What are JUnit tests?
    A test in JUnit is a method or class used only for testing. These are called  Test methods or Tests classes. The tests contain the code used for testing the behavior of the application.
     
  3. What are JUnit Assumptions?
    Assumptions in JUnit are a collection of utility methods that supports the execution of conditional test based on assumptions. Assumptions are used whenever we don’t want to continue the implementation of a given test method.
     
  4. What are the methods provided by Assumptions?
    Assumptions provide us with three methods that can be overloaded according to the parameters and conditions; Assumptions.assumeTrue(), Assumptions. false(), and Assumptions.assumingThat().
     
  5. What does assumeThat() method do?
    assumeThat() performs the tests if the conditions are evaluated to be true. Otherwise, id doesn't abort them but runs the rest of the code. 

Key Takeaways

In this article, we have discussed JUnit and learned how to use assumptions in our code to run the tests with conditions.
Hey Ninjas! We hope this blog helped you enhance your knowledge of JUnit and testing. If you want to learn more, check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass