Home Junit 5 Junit 5 Assumptions class examples

Junit 5 Assumptions class examples

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.

  1. Assumptions.assumeTrue() –  If the  condition is true, then run the test, else aborting the test.
  2. Assumptions.false() –  If the  condition is false, then run the test, else aborting the test.
  3. 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 :

  1. Junit 5.5.2
  2. Maven 3
  3. Java 8
  4. Spring Tool Suite 3.9.8

2.2 Example project structure :

Junit-5-assumptions-example

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 :

Junit 5 assumptions 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 :

  1. Junit 5 Dynamic Tests and @TestFactory annotation.
  2. Junit 5 tags and filter test cases for execution.
  3. Junit 5 Timeout tests, fail tests if not completed within time.
  4. Junit 5 Repeated tests and display Repetition info.
  5. Junit 5 conditional tests execution examples
  6. Junit 5 Disable tests using @Disabled annotation

LEAVE A REPLY

Please enter your comment!
Please enter your name here