JUnit 5 Unit testing involves the testing of each unit or an individual component of the software application. It is the first level of fu...
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
JUnit Platform
JUnit Jupiter
This module includes new programming and extension models for writing tests in JUnit 5. New annotations 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)
JUnit Vintage
JUnit Vintage supports running tests based on JUnit 3 and JUnit 4 on the JUnit 5 platform.
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); }); }
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><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-runner</artifactId><version>${junit.platform.version}</version><scope>test</scope></dependency></dependencies>
COMMENTS