Parameterized Test
This Parameterized Test is used to test a Test case with different parameters for this we use @ParameterizedTest annotations.
Example of Parameterized Test:
//Java program to demonstrate parameterized test
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MyParameterizedTest {
@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 4, 5})
void testSquare(int value) {
int result = square(value);
assertEquals(value * value, result, "Square calculation is incorrect");
}
private int square(int number) {
return number * number;
}
}
Introduction to JUnit 5
JUnit is a Testing Framework. The Junit 5 is the latest version of the testing framework, and it has a lot of features when compared with Junit 4. JUnit 5, also known as JUnit Jupiter. It introduces several new features and improvements over its predecessor, JUnit 4, making it more powerful and flexible for writing and running tests.
The JUnit 5 version has three different modules for defining and performing different functionalities in testing. The components are:
- JUnit Platform
- JUnit Jupiter
- JUnit Vintage
These three components play an important role in writing test cases for a software application. Now we will discuss each component in simple words.
JUnit Platform
In Software Industries, The Testing team designs and develops different types of test cases for checking the quality of the application. This means they test application functionality and Behavior in different conditions. After the Test cases are Developed, where we run those test cases. The Answer is, In Java If we want to run the test cases, we need JVM. So, the JUnit Platform provides a launching mechanism for testing frameworks on the JVM.
JUnit Jupiter
The second component in Junit 5 is JUnit Jupiter. This component provides new programming techniques for developing the test cases in JUnit 5.
JUnit Vintage
The Third component is JUnit Vintage in JUnit 5. The JUnit Vintage functionality is different from the above Two. Before JUnit 5, The Tester uses JUnit 4, JUnit 3, or some other Versions. But nowadays everybody shows interest in JUnit 5 for developing test cases. The Main functionality of JUnit Vintage is allowing JUnit 3 and JUnit 4 Test cases on the JUnit 5 Platform.
The JUnit Provides a lot of features when compared with JUnit 4. If want to learn JUnit 5 then we need to know some basics of the JUnit 5 Framework. Now I provide the basic information about JUnit 5. The basics of JUnit 5 are,
- Annotations
- Test Life cycle methods
- Assertions
- Assumptions
- Parameterized Test
- Dynamic Tests
- Tagging and Filtering
Maven Dependency for JUnit 5
To use JUnit 5 with Maven, you need to add the following dependencies to your pom.xml
:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
Gradle Dependency for JUnit 5
To use JUnit 5 with Gradle, you need to add the following dependencies to your build.gradle
:
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.3'
testImplementation 'org.junit.vintage:junit-vintage-engine:5.9.3'