Skip to content

Getting started

Andreas Schmid edited this page Aug 15, 2014 · 17 revisions

Requirements

This JUnit dataprovider requires JUnit in version 4.8.2+ (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.11 and junit-dataprovider in version 1.9.0. Please change this according to your preferences:

Gradle

build.gradle :

repositories {
    mavenCentral()
}

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

Maven

pom.xml :

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

Ivy

ivy.xml :

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

Simple usage example

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);
    }
}