Skip to content

Getting started

Andreas Schmid edited this page Nov 12, 2017 · 17 revisions

TODO provide features of JUnit5 (see Migration guides for now). Sorry for the inconvenience.


Requirements

The junit-dataprovider requires a JVM-compatible development environment, such as Java, Groovy, Scala, ...

Furthermore, you have to provide JUnit in version 4.8.2+ as dependency (see junit-dep-4.8.2 / junit-4.8.2). If you are using a previous version and cannot upgrade, please let us know by opening an issue here.

Download

All released (= tagged) versions are available at Maven Central Repository. Following this link you can choose a version. For more information about a certain version, see release notes. Now either download it manually or see the Dependency Information section how to integrate it with your dependency management tool or use one of the following excerpts for the most populare Java build tools:

Note: The following example excerpts use JUnit in version 4.12 and junit-dataprovider in version 1.10.0. Please change this according to your preferences:

Gradle

build.gradle :

repositories {
    mavenCentral()
}

dependencies {
    // ...
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile group: 'com.tngtech.java', name: 'junit-dataprovider', version: '1.10.0'
    // ...
}

Maven

pom.xml :

    <dependencies>
        <!-- ... -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.tngtech.java</groupId>
            <artifactId>junit-dataprovider</artifactId>
            <version>1.10.0</version>
            <scope>test</scope>
        </dependency>
        <!-- ... -->
    </dependencies>

Ivy

ivy.xml :

    <dependencies>
        <!-- ... -->
        <dependency org="junit" name="junit" rev="4.12" />
        <dependency org="com.tngtech.java" name="junit-dataprovider" rev="1.10.0" />
        <!-- ... -->
    </dependencies>

Usage

After including one of the above excerpts in your build file and refreshing your IDE, you simple can create a new JUnit test and use the junit-dataprovider:

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;

@RunWith(DataProviderRunner.class)
public class DataProviderTest {

    @DataProvider
    public static Object[][] dataProviderAdd() {
        // @formatter:off
        return new Object[][] {
                { 0, 0, 0 },
                { 1, 1, 2 },
                /* ... */
        };
        // @formatter:on
    }

    @Test
    @UseDataProvider("dataProviderAdd")
    public void testAdd(int a, int b, int expected) {
        // Given:

        // When:
        int result = a + b;

        // Then:
        assertEquals(expected, result);
    }
}

For more features and customization possibilities, see Features.