JUnit 5 introduced several new annotations that enhance the flexibility and functionality of writing tests. These annotations provide more control over test execution and allow for more expressive and maintainable test code. In this section, we will explore these new annotations, their purposes, and how to use them effectively.
- @Test
The @Test
annotation is used to denote a method as a test method. This is similar to JUnit 4, but JUnit 5's @Test
is part of the org.junit.jupiter.api
package.
import org.junit.jupiter.api.Test; public class MyTests { @Test void testMethod() { // Test logic here } }
- @DisplayName
The @DisplayName
annotation allows you to provide a custom name for your test methods. This can make your test reports more readable and descriptive.
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class MyTests { @Test @DisplayName("Test addition of two numbers") void testAddition() { // Test logic here } }
- @BeforeEach and @AfterEach
These annotations are used to specify methods that should be executed before and after each test method, respectively. They replace JUnit 4's @Before
and @After
.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; public class MyTests { @BeforeEach void setUp() { // Initialization logic here } @AfterEach void tearDown() { // Cleanup logic here } @Test void testMethod() { // Test logic here } }
- @BeforeAll and @AfterAll
These annotations are used to specify methods that should be executed once before all tests and once after all tests, respectively. They replace JUnit 4's @BeforeClass
and @AfterClass
. Methods annotated with @BeforeAll
and @AfterAll
must be static.
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; public class MyTests { @BeforeAll static void initAll() { // Initialization logic here } @AfterAll static void tearDownAll() { // Cleanup logic here } @Test void testMethod() { // Test logic here } }
- @Nested
The @Nested
annotation allows you to create nested test classes, which can help organize tests into logical groups. Nested test classes can have their own lifecycle methods.
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; public class MyTests { @Nested class InnerTests { @Test void innerTestMethod() { // Test logic here } } }
- @Tag
The @Tag
annotation is used to tag test methods or classes. This can be useful for filtering tests during execution.
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; public class MyTests { @Test @Tag("fast") void fastTest() { // Fast test logic here } @Test @Tag("slow") void slowTest() { // Slow test logic here } }
- @Disabled
The @Disabled
annotation is used to disable a test method or class. This is similar to JUnit 4's @Ignore
.
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; public class MyTests { @Test @Disabled("Disabled until bug #123 is fixed") void disabledTest() { // This test will not run } }
- @RepeatedTest
The @RepeatedTest
annotation is used to run a test multiple times. This can be useful for testing idempotency or performance.
import org.junit.jupiter.api.RepeatedTest; public class MyTests { @RepeatedTest(5) void repeatedTest() { // This test will run 5 times } }
- @ParameterizedTest
The @ParameterizedTest
annotation is used to run a test with different parameters. This is a powerful feature for testing with multiple sets of data.
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; public class MyTests { @ParameterizedTest @ValueSource(strings = {"Hello", "JUnit"}) void parameterizedTest(String word) { // This test will run with "Hello" and "JUnit" } }
Summary
JUnit 5 introduces several new annotations that provide more flexibility and control over test execution. These annotations help in writing more expressive, maintainable, and organized test code. Understanding and utilizing these annotations effectively can significantly improve your testing practices.
In the next topic, we will explore how to migrate from JUnit 4 to JUnit 5, ensuring a smooth transition and leveraging the new features provided by JUnit 5.
JUnit Course
Module 1: Introduction to JUnit
Module 2: Basic JUnit Annotations
- Understanding @Test
- Using @Before and @After
- Using @BeforeClass and @AfterClass
- Ignoring Tests with @Ignore
Module 3: Assertions in JUnit
Module 4: Parameterized Tests
- Introduction to Parameterized Tests
- Creating Parameterized Tests
- Using @ParameterizedTest
- Custom Parameterized Tests
Module 5: Test Suites
Module 6: Mocking with JUnit
Module 7: Advanced JUnit Features
Module 8: Best Practices and Tips
- Writing Effective Tests
- Organizing Test Code
- Test-Driven Development (TDD)
- Continuous Integration with JUnit