I remember a year ago I was complaining about JUnit 4 not having the Dataprovider functionality like TestNG has - see .
We had to use a third party library com.tngtech.java:junit-dataprovider
That problem was resolved in JUnit 5.
Now in JUnit5 you can write TestNG style parameterized tests from the box.
Required JUnit 5 maven dependencies
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.3.2</version>
</dependency>
TestNG like parameterized tests in JUnit 5 example
Data source
The “Dataprovider” method in JUnit 5 looks similar to the TestNG Dataprovider method just without @Dataprovider
public static Object[][] sumTestData() {
return new Object[][]{
{2, 2, 4},
{10, 1, 11},
{1000000, -1000000, 0}
};
}
Parameterized JUnit 5 test.
public static Object[][] sumTestData() {
return new Object[][]{
{2, 2, 4},
{10, 1, 11},
{1000000, -1000000, 0}
};
}
@ParameterizedTest
@MethodSource("sumTestData")
public void dataProviderTest(int a, int b, int expectedSum) {
Assertions.assertEquals(expectedSum, a + b);
}
You may also find these posts interesting: