假设

JUnit Jupiter附带了JUnit4提供的一些assumption方法的子集,并增加了一些适合与Java 8 lambda一起使用的方法。所有的JUnit Jupiter assumption都是org.junit.jupiter.Asumptions类中的静态方法。

  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 org.junit.jupiter.api.Test;
  5. class AssumptionsDemo {
  6. @Test
  7. void testOnlyOnCiServer() {
  8. assumeTrue("CI".equals(System.getenv("ENV")));
  9. // remainder of test
  10. }
  11. @Test
  12. void testOnlyOnDeveloperWorkstation() {
  13. assumeTrue("DEV".equals(System.getenv("ENV")),
  14. () -> "Aborting test: not on developer workstation");
  15. // remainder of test
  16. }
  17. @Test
  18. void testInAllEnvironments() {
  19. assumingThat("CI".equals(System.getenv("ENV")),
  20. () -> {
  21. // perform these assertions only on the CI server
  22. assertEquals(2, 2);
  23. });
  24. // perform these assertions in all environments
  25. assertEquals("a string", "a string");
  26. }
  27. }