Skip to content

Getting started

Andreas Schmid edited this page May 11, 2020 · 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, ... and you have to provide JUnit.

junit-dataprovider 1.*

junit-dataprovider 2.*

  • since 2.0
  • compatible with JUnit4 (JVM 5.0 and above) and JUnit5 (JVM 8 and above)

JUnit4

JUnit5

For concrete JUnit5 versions, see release notes.

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.junit.dataprovider', name: 'junit4-dataprovider', version: '2.6'
    // ...
}

Maven

pom.xml :

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

Ivy

ivy.xml :

    <dependencies>
        <!-- ... -->
        <dependency org="junit" name="junit" rev="4.12" />
        <dependency org="com.tngtech.junit.dataprovider" name="junit4-dataprovider" rev="2.6" />
        <!-- ... -->
    </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.