JUnit 5
Unit testing involves the testing of each unit or an individual component of the software application. It is the first level of functional testing. The aim behind unit testing is to validate unit components with their performance.
JUnit5 main aim is to adapt the Java 8 style of coding. Minimum Jdk8 is required to execute JUnit 5 tests.
Architecture
JUnit 5 contains a different set of modules from its subprojects.
- JUnit Platform
- JUnit Jupiter
- JUnit Vintage
1. JUnit Platform
The platform is responsible for launching testing frameworks on the JVM (Java Virtual Machine). It defines a stable and powerful interface between JUnit and its clients, such as build tools.
The platform easily integrates clients with JUnit to discover and execute tests.
It also defines the TestEngine API (Application Program Interface) for developing a testing framework that runs on the JUnit platform. By implementing a custom TestEngine, we can plug 3rd party testing libraries directly into JUnit.
2. JUnit Jupiter
This module includes new programming and extension models for writing tests in JUnit 5. New annotations (whatever starts with '@') in comparison to JUnit 4 are:
@TestFactory – denotes a method that's a test factory for dynamic tests
@DisplayName – defines a custom display name for a test class or a test method
@Nested – denotes that the annotated class is a nested, non-static test class
@Tag – declares tags for filtering tests
@ExtendWith – registers custom extensions
@BeforeEach – denotes that the annotated method will be executed before each test method (previously @Before)
@AfterEach – denotes that the annotated method will be executed after each test method (previously @After)
@BeforeAll – denotes that the annotated method will be executed before all test methods in the current class (previously @BeforeClass)
@AfterAll – denotes that the annotated method will be executed after all test methods in the current class (previously @AfterClass)
@Disable – disables a test class or method (previously @Ignore)
3. JUnit Vintage
JUnit Vintage supports running tests based on JUnit 3 and JUnit 4 on the JUnit 5 platform. Basically testing in a backward compatibility.
4. Basic Annotation:-
Example 1
@Test
void testSingleSuccessTest() {
System.out.println("Success Test");
}
@BeforeAll
static void setup() {
System.out.println("@BeforeAll - executes once before all test methods in this class");
}
@BeforeEach
void init() {
System.out.println("@BeforeEach - executes before each test method in this class");
} Example 2
@DisplayName("Single test successful")
@Test
void testSingleSuccessTest() {
System.out.println("Success Test");
}
@Disabled("Not implemented yet")
@Test
void avoidTesting() {
}Example 3
@Test
void SingleAssertion()
{
assertEquals(2,3);
}
@Test
void groupAssertions() {
int[] numbers = {0, 1, 2, 3, 4};
assertAll("numbers",
() -> assertEquals(numbers[0], 1),
() -> assertEquals(numbers[3], 3),
() -> assertEquals(numbers[4], 1)
);
}Example 4
@Test
void shouldThrowException() {
Throwable exception = assertThrows(UnsupportedOperationException.class, () -> {
throw new UnsupportedOperationException("Not supported");
});
assertEquals(exception.getMessage(), "Not supported");
}
@Test
void assertThrowsException() {
String str = null;
assertThrows(IllegalArgumentException.class, () -> {
Integer.valueOf(str);
});
}