Updated April 1, 2023
What is JUnit Jupiter?
JUnit Jupiter offers new Programming and Extension model for developing tests as well as extensions. It is a critical component in the making of JUnit 5 along with JUnit Platform, a foundational Testing framework along with classical JUnit Vintage component which provides necessary TestEngines for executing JUnit 3 / JUnit 4 based tests.
Along with being an Extension model, JUnit Jupiter provides a TestEngine to execute Jupiter-based tests on this platform. These TestEngines identify the correct Tests for various programming models and execute them using their own ID and discover tests provided by the EngineRecoveryRequests.
Writing Tests in JUnit Jupiter
Tests are developed in Jupiter as a method that is contained in an exclusive class used for testing. This class is known as Test Class. In order to identify a method as test method, there has to be an annotation @Test. The code under this method is executed during the testing.
Assert methods are used in the code to verify the actual result with the intended result and such assert statements also enable to display of messages whenever the test fails. Developer can code meaningful messages when they create tests, so that users will understand the error messages easily and take action easily.
JUnit Jupiter Framework
Annotation and Assert methods are the most important constituents of the JUnit Jupiter framework.
Annotation | Description |
@Test | This annotation clearly indicates the subject method is a Test Method. Test Extension in Jupiter has its own annotations and hence this annotation does not declare any attribute unlike JUnit 4. These methods are generally inherited unless overridden. |
@TestFactory | This method is meant for dynamic tests and is normally inherited unless it is overridden. |
@RepeatedTest | This method acts as a template for performing tests repetitively. |
@ParameterizedTest | This indicates that this method enables executing the same test multiple times using different datasets.
|
@TestTemplate | This method is a template for test cases that are designed to be invoked many times based on the invocation contexts returned by registered providers. |
@TestClassOrder | This method helps to configure test execution order in the case of @Nested test classes. These annotations are inherited |
@DisplayName | Test Method or classes are identified by custom display names. These annotations are not inherited. |
@BeforeEach | It indicates that such annotated methods should be executed prior to each and every @Test, @ParameterizedTest, @TestFactory or @RepatedTest method. The equivalent of this method in JUnit 4 is @Before |
@AfterEach | It indicates that such annotated methods should be executed post each and every @Test, @TestFactory, @ParameterizedTest or @RepatedTest method. The equivalent of this method in JUnit 4 is @After |
@BeforeAll | It indicates that such annotated methods should be executed prior to all @Test, @ParameterizedTest, @TestFactory, or @RepatedTest methods. Equivalent of this method in JUnit 4 is @BeforeClass |
@AfterAll | It indicates that such annotated methods should be executed post all @Test, @ParameterizedTest, @TestFactory, or @RepatedTest methods. Equivalent of this method in JUnit 4 is @AfterClass |
Assertions and Assumptions
There are multiple assert statements in JUnit 5 which enables us to verify the test results and ascertain whether the code works properly or not. They are all static methods originating from the package org.JUnit.jupiter.API.assertions.*. These assert statements cover test conditions of equality, true or false, null or not null.
Assert variant | Usage |
asssertEquals | It checks for equal condition of the result returned by the code with the intended value.
assertEquals(36, Multiplier.product(6 , 6), “Failed Result Text”); |
assertTrue | It checks whether the given condition is true or not
assertTrue(‘x’ < ‘y’, () , “Failed Result Text”); |
assertFalse | It checks whether the given condition is false or otherwise
assertFalse(‘x’ > ‘y’, () , “Failed Result Text”); |
assertNull | It verifies whether a data variable has a null value or not
assertNull(Subject, “Failed Result Text”); |
assertNotNull | It verifies whether a data variable has a value or not
assertNotNull(Subject, “Failed Result Text”); |
Testing occurrence of Exception conditions
org.JUnit.jupiter.API.Assertions.expectThrows() assert statement tests that some exceptions are thrown during the testing. It tracks any such exceptions which are pre-defined and provides code to handle the exception upon its occurrence.
JUnit Jupiter Extension Model
As against extension points in JUnit 4 such as Runner, MethodRule, and TestRule, Jupiter extension model has a single comprehensive concept called Extension API. It has to be noted that this Extension is only a marker interface. There are various registration processes with extensions and they are explained below briefly.
- Declarative Registration: By annotating a test method/test class/test interface with @ExtendWith, Developers can register one or many extensions. Class references should also be supplied to complete the extension registration.
- Programmatic Registration: By annotating fields with @RegisterExtension in test classes, Developers can register extension programmatically. Unlike declarative registration where configuration is possible only through annotation, here it can be configured programmatically.
- Automatic Registration: Java’s ServiceLoader mechanism enables automatic detection of third-party extensions and their registration.
Classes and Methods
- Classes
Classes are it top-level class or @Nested class or static member class, It should have at least one test method, must contain a single constructor and it should not be abstract.
- Methods
Methods are annotated directly or meta-annotated with @Test, @ParameterizedTest, @TestTemplate, @RepeatedTest. There are lifecycle methods that are annotated or meta-annotated with @BeforeEach, @BeforeAll, @AfterEach, @AfterALL
Examples
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.AfterAll;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Disabled;
class StandardTests {
@BeforeAll
static void initAll() {
}
@BeforeEach
void init() {
}
@Test
void succeedingTest() {
}
@Test
void failingTest() {
fail("Test Fails");
}
@Test
@Disabled("Demo only")
void skippedTest() {
}
Conclusion
JUnit Jupiter is ready to use, easy to use, and simple to implement. It has brand new features and it is an improved version of JUnit 4 test cases. It facilitates developers to achieve quicker project builds and helps in managing lifecycle efficiently.
Recommended Articles
This is a guide to JUnit Jupiter. Here we discuss the What is JUnit Jupiter, JUnit Jupiter Framework, methods, and examples. You may also have a look at the following articles to learn more –