A TestNG-like dataprovider runner for JUnit having a simplified syntax compared to all the existing JUnit features.
npx @tessl/cli install tessl/maven-com-tngtech-java--junit-dataprovider@1.13.0JUnit DataProvider is a Java testing library that extends JUnit 4 with TestNG-style data provider functionality, enabling parameterized tests with enhanced syntax and flexibility. The library provides annotations (@DataProvider, @UseDataProvider) that allow test methods to receive data from various sources including arrays, iterables, and string-based formats, supporting complex test scenarios with multiple data sets.
Maven:
<dependency>
<groupId>com.tngtech.java</groupId>
<artifactId>junit-dataprovider</artifactId>
<version>1.13.1</version>
<scope>test</scope>
</dependency>Gradle:
testImplementation 'com.tngtech.java:junit-dataprovider:1.13.1'import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.DataProviderFilter;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import com.tngtech.java.junit.dataprovider.Placeholders;
import org.junit.Test;
import org.junit.runner.RunWith;The most common usage pattern involves defining a data provider method and using it in a test:
@RunWith(DataProviderRunner.class)
public class ExampleTest {
@DataProvider
public static Object[][] stringLengthData() {
return new Object[][] {
{ "hello", 5 },
{ "world", 5 },
{ "", 0 },
{ "testing", 7 }
};
}
@Test
@UseDataProvider("stringLengthData")
public void testStringLength(String input, int expectedLength) {
assertEquals(expectedLength, input.length());
}
}The foundation of JUnit DataProvider functionality with the custom runner and core annotations.
@RunWith(DataProviderRunner.class)
public class TestClass {
@DataProvider
public static Object[][] dataProviderMethod() { /* ... */ }
@Test
@UseDataProvider("dataProviderMethod")
public void testMethod(/* parameters */) { /* ... */ }
}Key APIs:
DataProviderRunner - Custom JUnit runner that enables data provider functionality@DataProvider - Annotation for marking data provider methods or providing inline data@UseDataProvider - Annotation for test methods to specify which data provider to useSupport for multiple data formats including arrays, iterables, and string-based data providers.
// Array format
@DataProvider
public static Object[][] arrayData() {
return new Object[][] { {"test", 4}, {"hello", 5} };
}
// Iterable format
@DataProvider
public static Iterable<Object[]> iterableData() {
return Arrays.asList(new Object[]{"test", 4}, new Object[]{"hello", 5});
}
// String format with inline data
@Test
@DataProvider({"test, 4", "hello, 5"})
public void testWithInlineData(String input, int length) { /* ... */ }Key features:
Data Provider Formats and Types
Utility classes and methods for creating and manipulating data provider arrays.
import static com.tngtech.java.junit.dataprovider.DataProviders.*;
// Helper methods for creating data
Object[] row = $("param1", "param2", 123);
Object[][] data = $$($("test1", 1), $("test2", 2));
// Utility methods
Object[][] enumTests = DataProviders.testForEach(MyEnum.class);
Object[][] crossProduct = DataProviders.crossProduct(data1, data2);Key utilities:
Utility Methods and Helper Classes
Advanced features including custom data provider method resolvers and test name formatting.
// Custom resolver
@UseDataProvider(value = "customName",
location = OtherClass.class,
resolver = CustomDataProviderMethodResolver.class)
// Custom formatting
@DataProvider(format = "%m[%i: %p[0]]")
public static Object[][] formattedData() { /* ... */ }
// Custom placeholders
Placeholders.all().add(0, new CustomPlaceholder());Key features: