In our previous post we figured out how to use DataProviders in JUnit tests - for some reason JUnit doesn’t support that from the box.

Fortunately there is

Unfortunately attempts to use that in Spring driven tests will fail. It doesn’t work with Spring.


Spent some time on searching on the internet for possible solutions and found there is only one option - we need a different test runner. Yay! - a workaround for a workaround.

Note: The code below is the solution that I found on the internet and copied-pasted without any change. Don’t know who the author is - if you know the author let me know, I can add the link/reference here.

JUnit DataProvider with Spring example

The complete work JUnit DataProvider with Spring example that uses our custom SpringDataProviderRunner

JUnit DataProviders Maven dependency

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


@SpringBootTest(classes = TestsContext.class)
public class JUnitDataProviderWithSpringExampleIT {

    public static Object[][] sumTestData() {
        return new Object[][]{
                {2, 2, 4},
                {10, 1, 11},
                {1000000, -1000000, 0}

    public void dataProviderTest(int a, int b, int expectedSum) {
        Assert.assertEquals(expectedSum, a + b);


Yes, doesn’t look very elegant but… this is America JUnit

You may also find these posts interesting: