JUnit 5 Quick Reference

Basic Annotations

@Test - Declare a test method

  @Test
  public void method(){}

@DisplayName - Provide a customized text on a test method/class

  @Test
  @DisplayName("Custom test name")
  public void method(){}

@BeforeEach - execute before each test method
@AfterEach - execute after each test method
Useful to do setup and clean up, before/after every test.

  @BeforeEach
  public void method1(){}
  @AfterEach
  public void method2(){}

@BeforeAll - execute once before all test methods
@AfterAll - execute once after all test methods
Useful to do setup and clean up, before/after all test cases.

  @BeforeAll
  public void method1(){}
  @AfterAll
  public void method2(){}

@Disabled - Disable test method

  @Test
  @Disabled
  public void method(){}

@Tag - to filter out test cases

  @Tag("web")
  public void method(){}

@RepeatedTest - to repeat test execution multiple times

  @Test
  @RepeatedTest(5) // execute's five times
  public void method(){}
Assertions

assertEquals - Check for equality. Works with any Primitives, Primitive wrapper classes, or any object which implements equals.

  org.junit.jupiter.api.Assertions.assertEquals(3, 1 + 2);

assertTrue - Except true from test expression
assertFalse - Except false from test expression

  org.junit.jupiter.api.Assertions.assertTrue(3 > 2, 
      () -> "failure message");
  org.junit.jupiter.api.Assertions.assertFalse(2 > 3, 
      () -> "failure message");

assertTimeout - If a given test takes longer than expected, then the test fails

  org.junit.jupiter.api.Assertions.assertTimeout(Duration.ofMinutes(1), () -> { 
    // test
  });

assertThrows - Verify if the provided executable throws an exception of the expected type (ArithmeticException.class) and returns the exception

  org.junit.jupiter.api.Assertions.assertThrows(ArithmeticException.class, 
      () -> divide(1, 0));

assertAll - Group assertions, so the execution result is shown together

  org.junit.jupiter.api.Assertions.assertAll("group-a", 
      () -> assertTrue(2 > 1),
      () -> assertEquals(3, 1 + 2));
Assumptions

assumeTrue - Assume expression will return true, if true then only execution continues

  org.junit.jupiter.api.Assumptions.assumeTrue(4 > 3);
  // continues execution only if expression returns true

assumeFalse - Assume expression will return false, if false then only execution continues

  org.junit.jupiter.api.Assumptions.assumeFalse(4 > 5);
  // continues execution only if expression returns false
Conditional Tests

@EnabledOnOs - Executes only on matched OS
@DisabledOnOs - Disabled on matched OS
Available OS: MAC, AIX, WINDOWS, LINUX & SOLARIS

  @Test
  @EnabledOnOs({OS.MAC, OS.WINDOWS}) // executes on MAC & Windows
  public void method1(){}

  @Test
  @DisabledOnOs(OS.WINDOWS) // disabled on Windows
  public void method2(){}

@EnabledOnJre - Executes only on matched JRE
@DisabledOnJre - Disabled on matched JRE
@EnabledForJreRange - Executes only on matched JRE range
@DisabledForJreRange - Disabled on matched JRE range
Available JRE: 8 to 16

  @Test
  @EnabledOnJre(JRE.JAVA_14) // executes on JRE 14
  public void method1(){}

  @Test
  @DisabledOnJre(JRE.JAVA_11) // disabled on JRE 11
  public void method2(){}
  
  @Test
  @EnabledForJreRange(min = JAVA_9, max = JAVA_10) // enabled from 9 to 11
  public void method3(){}

  @Test
  @DisabledForJreRange(min = JAVA_9, max = JAVA_11) // disabled from 9 to 11
  public void method4(){}

@EnabledIfSystemProperty - Execute if value of the provided system property matches
@DisabledIfSystemProperty - Disabled if value of the provided system property matches

  @Test
  @EnabledIfSystemProperty(named = "test-env", matches = "dev")
  public void method1(){}

  @Test
  @DisabledIfSystemProperty(named = "test-env", matches = "perf")
  public void method2(){}

@EnabledIfEnvironmentVariable - Execute if value of the provided environment variable matches
@DisabledIfEnvironmentVariable - Disabled if value of the provided environment variable matches

  @Test
  @EnabledIfEnvironmentVariable(named = "test-env", matches = "dev")
  public void method1(){}

  @Test
  @DisabledIfEnvironmentVariable(named = "test-env", matches = "perf")
  public void method2(){}

@EnabledIf - Execute if provided condition is true
@DisabledIf - Disabled if provided condition is true

  @Test
  @EnabledIf("#{<spel_expression>}")
  public void method1(){}

  @Test
  @DisabledIf("#{<spel_expression>}")
  public void method2(){}
comments powered by Disqus