2.5. Assumptions

JUnit Jupiter comes with a subset of the assumption methods that JUnit 4 provides and adds a few that lend themselves well to being used with Java 8 lambda expressions and method references. All JUnit Jupiter assumptions are static methods in the [org.junit.jupiter.api.Assumptions](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/Assumptions.html) class.

  1. import static org.junit.jupiter.api.Assertions.assertEquals;
  2. import static org.junit.jupiter.api.Assumptions.assumeTrue;
  3. import static org.junit.jupiter.api.Assumptions.assumingThat;
  4. import example.util.Calculator;
  5. import org.junit.jupiter.api.Test;
  6. class AssumptionsDemo {
  7. private final Calculator calculator = new Calculator();
  8. @Test
  9. void testOnlyOnCiServer() {
  10. assumeTrue("CI".equals(System.getenv("ENV")));
  11. // remainder of test
  12. }
  13. @Test
  14. void testOnlyOnDeveloperWorkstation() {
  15. assumeTrue("DEV".equals(System.getenv("ENV")),
  16. () -> "Aborting test: not on developer workstation");
  17. // remainder of test
  18. }
  19. @Test
  20. void testInAllEnvironments() {
  21. assumingThat("CI".equals(System.getenv("ENV")),
  22. () -> {
  23. // perform these assertions only on the CI server
  24. assertEquals(2, calculator.divide(4, 2));
  25. });
  26. // perform these assertions in all environments
  27. assertEquals(42, calculator.multiply(6, 7));
  28. }
  29. }
As of JUnit Jupiter 5.4, it is also possible to use methods from JUnit 4’s org.junit.Assume class for assumptions. Specifically, JUnit Jupiter supports JUnit 4’s AssumptionViolatedException to signal that a test should be aborted instead of marked as a failure.