Here is a JUnit 5 Cheatsheet which can be used as handy reference to build your Tests in Java.

JUnit 5 cheatsheet

Maven Dependencies:

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Gradle:

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.2')
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.5.2')
}

Basic JUnit 5Test

class JUnit5ExampleTest {

    @org.junit.jupiter.api.Test
    void firstTest() {
        System.out.println("Example Test");
    }

}

Lifecycle:

  • @BeforeEach is used to signal that the annotated method should be executed before each @Test method in the current test class.
  • @AfterEach the annotated method should be executed after each test method.
  • @BeforeAll executes a method before all tests.
  • @AfterAll is used to execute the annotated method, only after all tests have been executed.

Assertions

@org.junit.jupiter.api.Test
void exampleTest() {
    Assertions.assertTrue(trueBool);
    Assertions.assertFalse(falseBool);

    Assertions.assertNotNull(notNullString);
    Assertions.assertNull(notNullString);

    Assertions.assertNotSame(originalObject, otherObject);
    Assertions.assertEquals(4, 4);
    Assertions.assertNotEquals(3, 2);

    Assertions.assertArrayEquals(new int[]{1,2,3}, new int[]{1,2,3}, "Array Equal Test");

    Iterable<Integer> listOne = new ArrayList<>(Arrays.asList(1,2,3,4));
    Iterable<Integer> listTwo = new ArrayList<>(Arrays.asList(1,2,3,4));
    Assertions.assertIterableEquals(listOne, listTwo);

    Assertions.assertTimeout(Duration.ofMillis(100), () -> {
    Thread.sleep(50);
    return "result";
    });

    Throwable exception = Assertions.assertThrows(IllegalArgumentException.class, () -> {
    throw new IllegalArgumentException("error message");
    });

    Assertions.fail("not found good reason to pass");
}

Assumptions

A failed assumption results in a test being aborted. Assumptions are typically used whenever it does not make sense to continue execution of a given test method.

 public void testOnDev() 
    {
        System.setProperty("ENV", "DEV");
        Assumptions.assumeTrue("DEV".equals(System.getProperty("ENV")));
        
        Assumptions.assumeFalse("DEV".equals(System.getProperty("ENV")), AppTest::message);
    }
    @END

Testsuite

Using JUnit 5 test suites, you can run tests spread into multiple test classes and different packages.

@RunWith(JUnitPlatform.class)
@SelectPackages("com.howtodoinjava.junit5.examples.packageA") 
public class JUnit5TestSuiteExample 
{
}

JUnit 5 cheatsheet

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials