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
//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'

Similar Reads

Annotations

The JUnit 5 framework uses different Annotations based on the test case design. Mostly in JUnit 5 @Test, @BeforeEach, @AfterEach, @BeforeAll, @AfterAll, @DisplayName, @Disabled these annotations are used. Basically, Annotations provides supplement information about the program in java. Annotations are always start with “@” symbol. ( reference )....

Assertions

The JUnit 5 provides different methods in Assertions class for checking the expected Result. Assertions are used to check if a condition is true. If the condition is false, the test fails. Common assertions include:...

Assumptions

Assumptions in JUnit 5 provides a good way for checking the test cases conditionally based on preconditions and one more is if Assumptions is failed then it is marked as skipped rather then failed. Means Assumptions are used to when you want to skip a test then we use Assumptions in JUnit 5....

Parameterized Test

This Parameterized Test is used to test a Test case with different parameters for this we use @ParameterizedTest annotations....

Dynamic Tests

JUnit 5 introduces the concept of dynamic tests, which can be generated at runtime. These are created using the DynamicTest class....

Tagging and Filtering

We can tag our test cases by using @tag annotation in the JUnit 5. Simply the Tags are labels for categorize the test cases. And Filter is used to filter and run the test cases by using the Tags....

@AfterAll, @AfterEach and @BeforeAll, @BeforeEach

Here we will understand one example for sum of two numbers by using such as @BeforeAll, @AfterAll, @BeforeEach, @AfterEach Annotations....

Conclusion

Before going to JUnit 5 we should know basic of testing methods as well as knowledge on JUnit 4. And we should know about Annotation in Spring Boot for testing related functionalities. Then only we can understand the functionality of JUnit 5 Testing Framework otherwise it is little bit difficult to understand It’s Functionality. But The JUnit 5 provides lot of features for application testing purpose when comparing with other frameworks....