JUnit is a popular testing framework in the Java ecosystem that simplifies writing and running tests. The @ParameterizedTest annotation in JUnit is used to run the same test with different inputs. This guide covers the basics of using the @ParameterizedTest annotation with @CsvFileSource to write parameterized tests in JUnit.
Table of Contents
- Introduction
- Steps to Create a JUnit Parameterized Test
- Real-World Use Case
- Conclusion
Introduction
The @ParameterizedTest annotation marks methods that should be executed multiple times with different parameters. The @CsvFileSource annotation is used to provide a CSV file as the source of the parameters for the parameterized test.
Steps to Create a JUnit Parameterized Test
Step 1: Add Maven Dependency
To use JUnit in your project, you need to add the JUnit dependency to your pom.xml file. Use the latest version of JUnit 5:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
Step 2: Create the CSV File
Create a CSV file that contains the parameters for the test. For example, create a file named addition.csv in the src/test/resources directory with the following content:
1,2,3
2,3,5
3,4,7
4,5,9
Step 3: Create the Class to be Tested
Create a Java class with methods that you want to test. For example, a simple MathUtils class:
public class MathUtils {
public int add(int a, int b) {
return a + b;
}
}
Step 4: Create the Parameterized Test Class
Create a test class in the src/test/java directory. Use the @ParameterizedTest annotation and specify the source of the parameters using @CsvFileSource.
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MathUtilsTest {
private final MathUtils mathUtils = new MathUtils();
@ParameterizedTest
@CsvFileSource(resources = "/addition.csv", numLinesToSkip = 0)
void testAddition(int a, int b, int expected) {
assertEquals(expected, mathUtils.add(a, b), a + " + " + b + " should equal " + expected);
}
}
In this example, the testAddition method is annotated with @ParameterizedTest and uses @CsvFileSource to provide the parameter sets from the addition.csv file. The test method will be executed once for each row in the CSV file.
Step 5: Run the Test
You can run the test using your IDE, Maven, or Gradle.
Using an IDE:
Most IDEs, like IntelliJ IDEA and Eclipse, have built-in support for running JUnit tests. Simply right-click on your test class or method and select “Run.”
Using Maven:
If you’re using Maven, you can run your tests with the following command:
mvn test
Using Gradle:
If you’re using Gradle, you can run your tests with the following command:
gradle test
Real-World Use Case
In real-world applications, you may need to test methods with various inputs stored in a CSV file. For instance, a StringUtils class might need to be tested with different string inputs and expected results.
Create the CSV File
Create a CSV file that contains the parameters for the test. For example, create a file named strings.csv in the src/test/resources directory with the following content:
hello,olleh
world,dlrow
java,avaj
,,
" "," "
Create the Class to be Tested
Create a Java class that contains business logic. For example, a StringUtils class that reverses strings:
public class StringUtils {
public String reverse(String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
}
In this class, the reverse method reverses a given string.
Create the Parameterized Test Class
Create a test class for the StringUtils in the src/test/java directory. Use the @ParameterizedTest annotation and @CsvFileSource to provide different string inputs and expected results:
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class StringUtilsTest {
private final StringUtils stringUtils = new StringUtils();
@ParameterizedTest
@CsvFileSource(resources = "/strings.csv", numLinesToSkip = 0)
void testReverse(String input, String expected) {
assertEquals(expected, stringUtils.reverse(input), "Reversing '" + input + "' should give '" + expected + "'");
}
}
In this test class, the testReverse method is annotated with @ParameterizedTest and uses @CsvFileSource to provide the parameter sets from the strings.csv file. The test method will be executed once for each row in the CSV file.
Running the Tests
You can run the tests using your IDE, Maven, or Gradle.
Using an IDE:
Most IDEs, like IntelliJ IDEA and Eclipse, have built-in support for running JUnit tests. Simply right-click on your test class or method and select “Run.”
Using Maven:
If you’re using Maven, you can run your tests with the following command:
mvn test
Using Gradle:
If you’re using Gradle, you can run your tests with the following command:
gradle test
Conclusion
The @ParameterizedTest annotation in JUnit makes it easy to run the same test with different inputs. By using @ParameterizedTest with @CsvFileSource, you can ensure that your methods behave correctly with various inputs stored in a CSV file without writing multiple test cases.