JUnit 5 Jupiter API Annotations
Overview
In this post, we will briefly discuss some of the new annotations in JUnit 5 Jupiter API. Jupiter API introduced many new annotations in JUnit 5. We will look at some of them here.
Jupiter api is used for writing tests. You can find all the classes under the package:
org.junit.jupiter.api
Sample Listing
Enviromnet used here is: JDK 12, Eclipse 2019.xx version and Windows 10 platform.
package test; import static org.junit.Assert.fail; /** * @author testingdocs * */ /* * Sample JUnit 5 test annotations */ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; // Sample Test class with JUnit 5 annotated methods public class JUnit5Annotations { @BeforeAll static void setUpOnce() { System.out.println("@BeforeAll annotated method -------||"); } @AfterAll static void tearDownOnce() { System.out.println("@AfterAll annotated method ---------||"); } @AfterEach void tearDownEachMethodOnce() { System.out.println("@AfterEach annotated method ~~~~~~~~~"); } @BeforeEach void setupEachMethodOnce() { System.out.println("@BeforeEach annotated method ~~~~~~~~~"); } @Test void testCodeMethodOne() { System.out.println("@Test annotated method -----> One"); } @Test void testCodeMethodTwo() { System.out.println("@Test annotated method -----> Two"); //fail("Fail this method"); } }
Run the tests. Right clickRun As >> JUnit test.
Output:
@BeforeAll annotated method ——-||
@BeforeEach annotated method ~~~~~~~~~
@Test annotated method —–> One
@AfterEach annotated method ~~~~~~~~~
@BeforeEach annotated method ~~~~~~~~~
@Test annotated method —–> Two
@AfterEach annotated method ~~~~~~~~~
@AfterAll annotated method ———||
Access voilations error can occur if you use modules. In that case add the requires clause to the module-info.java
requires org.junit.jupiter.api;