Initial commit of a Maven project demonstrating JUnit 5's @RepeatedTest annotation. Includes example tests for basic repetition, custom display names, metadata injection, lifecycle management, and failure thresholds. Project files include .gitignore, README, pom.xml, and test classes under src/test/java/com/ankurm/tutorials/junit/repeated.
JUnit 5 @RepeatedTest Demonstration
This project demonstrates the usage of JUnit 5's @RepeatedTest annotation to improve test reliability and handle flaky tests.
Concepts Covered
The code examples in src/test/java/com/ankurTutorials/junit/repeated cover the following concepts:
- Basic Repetition: Repeating a test a specific number of times.
- File:
ReliabilityTest.java
- File:
- Custom Display Names: Configuring formatted names for each repetition for better reporting in CI/CD.
- File:
CustomNameTest.java
- File:
- Metadata Injection: Using
RepetitionInfoto access the current repetition index and total count within the test.- File:
MetadataTest.java
- File:
- Lifecycle Management: Understanding how
@BeforeEachand@AfterEachinteract with repeated tests (they run for each repetition).- File:
LifecycleTest.java
- File:
- Failure Thresholds: Using the
failureThresholdattribute (JUnit 5.10+) to stop execution early if a certain number of failures occur.- File:
FlakyServiceTest.java
- File:
Prerequisites
- Java 17 or higher
- Maven 3.6+
How to Run
To run all tests, execute the following command in the project root:
mvn test
To run a specific test class:
mvn -Dtest=BasicUsageTest test