Example for Test Case2
Test Script: Let us write the first test case i.e. `Testcase2`.
Java
package project; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.RepeatedTest; class Testcase2 { @RepeatedTest (value= 3 , name= "Test run {currentRepetition} of {totalRepetitions}" ) public void test1() { Addition addition = new Addition(); int actual = addition.sum( 2 , 3 ); int expected = 5 ; assertEquals(actual, expected); System.out.println( "Test executed" ); } } |
Output of JUnit 5 Program
JUnit view:
Console view:
Explanation of the above Program:
In the above code snippet, the `Testcase2` is annotated with `@RepeatedTest()` along with the `value` as `3` and `name` as `Test run {currentRepetition} of {totalRepetitions}` representing that the test should execute 3 times and displaying the current and total repeatition values. we have defined the `test1` method in which we have created the `addition` object for `Addition.java` and calculated the sum and stored in `actual` variable. We have asserted the result by comparing the `actual` and `expected` values with `assertEquals` method.
JUnit 5 – @RepeatedTest
JUnit 5 is a widely used testing framework in the Java ecosystem. It is the successor of JUnit 4 and is designed to address its limitations. JUnit framework allows the developers to write and run the tests for their Java code. These tests help ensure that the code functions correctly and continues to work as expected as changes are made. JUnit 5 provides a variety of annotations and one such annotation is `@RepeatedTest`. In this article, let us understand about @RepeatedTest annotation in JUnit 5.
Prerequisites
To understand this, we need to have some prerequisites.
- It is essential to have knowledge of at least Java version 8.
- Understanding of build management tools like Maven or Gradle.
- Java Integrated Development Environment (IDE) such as Eclipse, IntelliJ IDEA, or Visual Studio Code.
Contact Us