40

I'm currently doing my first Java project and like to fully TDD it. I'm using JUnit for writing the tests. Apparently JUnit does not provide support for data providers, which makes it rather annoying to test the same method with 20 different versions of an argument. What is the most popular/standard testing tool for Java that does support data providers? I came across TestNG, but have no idea how popular that one is, or how it compares to alternatives.

If there is a way to get this behaviour is a nice way using JUnit, then that might also work.

4
  • 1
    JUnit has parameterized tests. Oct 5, 2013 at 23:23
  • But be warned, that the Eclipse integration for parametrized JUnit test sucks. a) It's not possible to run a single parameter set, b) it's hard to tell from the JUnit window, which parameter actually failed during testing (you see something like [0], [1], [2] in the list).
    – qqilihq
    Oct 5, 2013 at 23:27
  • 3
    @qqilihq JUnit 4.11 now has ways to better name parameterized tests so you don't have this kind of problem anymore github.com/junit-team/junit/blob/master/doc/ReleaseNotes4.11.md
    – dkatzel
    Oct 5, 2013 at 23:31
  • @dkatzel Thank you, will have a look!
    – qqilihq
    Oct 5, 2013 at 23:38

5 Answers 5

54

Coworkers of mine at our company wrote a freely available DataProvider in TestNG style for JUnit which you can find on github (https://github.com/TNG/junit-dataprovider).

We use it in very large projects and it works just fine for us. It has some advantages over JUnit's Parameterized as it will reduce the overhead of separate classes and you can execute single tests as well.

An example looks something like this

@DataProvider
public static Object[][] provideStringAndExpectedLength() {
    return new Object[][] {
        { "Hello World", 11 },
        { "Foo", 3 }
    };
}

@Test
@UseDataProvider( "provideStringAndExpectedLength" )
public void testCalculateLength( String input, int expectedLength ) {
    assertThat( calculateLength( input ) ).isEqualTo( expectedLength );
}

Edit: Since v1.7, it also supports other ways to provide data (strings, lists) and can inline the provider so that a separate method is not necessarily needed.

A full, working example can be found on the manual page on github. It also has a few more features, like collecting the providers in utility classes and accessing them from other classes etc. The manual page is very detailed, I'm sure you'll find any questions answered there.

1
  • 7
    For those of you trying to run the example above: don't forget to add @RunWith(DataProviderRunner.class) annotation to your test class!
    – ADi3ek
    Feb 5, 2018 at 18:57
40

JUnit 4 has parameterized test which is the does the same thing as php data providers

@RunWith(Parameterized.class)
public class MyTest{ 
     @Parameters
    public static Collection<Object[]> data() {
           /*create and return a Collection
             of Objects arrays here. 
             Each element in each array is 
             a parameter to your constructor.
            */

    }

    private int a,b,c;


    public MyTest(int a, int b, int c) {
            this.a= a;
            this.b = b;
            this.c = c;
    }

    @Test
    public void test() {
          //do your test with a,b
    }

    @Test
    public void testC(){
        //you can have multiple tests 
        //which all will run

        //...test c
    }
}
4
  • 10
    This is similar, though not the same. The PHP data providers pass arguments to your methods. In your example, arguments are passed to the constructor of the test class. What if I have multiple test methods for which I want to have a list of values? Creating a test class for each would be very awkward. Oct 6, 2013 at 15:13
  • You can have multiple test methods each annotated with a @Test. All tests will be exercised. I have slightly updated my answer to show how
    – dkatzel
    Oct 6, 2013 at 20:33
  • 11
    That is good for when you have multiple tests that use the same list of arguments or elements from the same list. If there is no overlap, it does not really help. In my case I have a list of valid inputs, and a list of invalid inputs, for which there is no overlap. Putting those together would be extremely odd. Oct 7, 2013 at 10:56
  • 1
    @JeroenDeDauw data providers are primarily for testing various scenarios of a single test without duplicating the code involved while allowing every scenario to run despite any given scenario failing. If you can build data providers such that multiple distinct tests can use them, then that's lagniappe.
    – andyfeller
    Dec 20, 2016 at 17:04
9

Depending on your needs in flexibility vs readability, you can choose Parameterized - junit's built in option, described by dkatzel. Other options are external junit runners provided by external libraries like zohhak, which let's you do:

 @TestWith({
        "clerk,      45'000 USD, GOLD",
        "supervisor, 60'000 GBP, PLATINUM"
    })
    public void canAcceptDebit(Employee employee, Money money, ClientType clientType) {
        assertTrue(   employee.canAcceptDebit(money, clientType)   );
    }

or junitParams with a bit different functionality. just pick whatever suits you the most

0
7

You can use JUnit 5's ParameterizedTest. Here's an example from https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-writing-parameterized-tests/ :

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
 
import java.util.stream.Stream;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
 
@DisplayName("Should pass the method parameters provided by the sumProvider() method")
class MethodSourceExampleTest {
 
    @DisplayName("Should calculate the correct sum")
    @ParameterizedTest(name = "{index} => a={0}, b={1}, sum={2}")
    @MethodSource("sumProvider")
    void sum(int a, int b, int sum) {
        assertEquals(sum, a + b);
    }
 
    private static Stream<Arguments> sumProvider() {
        return Stream.of(
                Arguments.of(1, 1, 2),
                Arguments.of(2, 3, 5)
        );
    }
}

It's possible to load test parameters from an annotation, a method or even a CSV file.

4

Here is another option. You don't have to use Google Guava, that is just my implementation.

This uses the same @Parameters as @dkatzel's answer, but instead of the class taking the arguments, the @Parameters annotation goes on specific test methods, so you can pick and choose which methods use that set of arguments.

import java.util.Collection;

import com.google.common.collect.ImmutableList;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(JUnitParamsRunner.class)
public class FrobTester {
    @SuppressWarnings("unused")
    private Collection validfrobAndGorpValues() {
        return ImmutableList.of(
            new Object[] {"frob28953", 28953},
            new Object[] {"oldfrob-189-255", 1890255}
        );
    }

    @Test
    @Parameters(method = "validfrobAndGorpValues")
    public void whenGivenFrobString_thenGorpIsCorrect(
        String frobString,
        int expectedGorpValue
    ) {
        // Arrange
        Frob frob = new Frob(frobString);

        // Act
        var actualGorpValue = frob.getGorpValue();

        // Assert
        Assert.assertEquals(actualGorpValue, expectedGorpValue);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.