82

Is there there any way to tell JUnit to run a specific test case multiple times with different data continuously before going on to the next test case?

1
  • 6
    By using theories (as pointed out by @dfa) Junit will treat all the tests as one test & will make it harder to figure out which test has failed. Whereas Parameterized Tests (as pointed out by@jjnguy) treat them all as different tests, clearly indicating which test failed.
    – mindreader
    May 2, 2014 at 18:12

9 Answers 9

67

take a look to junit 4.4 theories:

import org.junit.Test;
import org.junit.experimental.theories.*;
import org.junit.runner.RunWith;

@RunWith(Theories.class)
public class PrimeTest {

    @Theory
    public void isPrime(int candidate) {
          // called with candidate=1, candidate=2, etc etc  
    }

    public static @DataPoints int[] candidates = {1, 2, 3, 4, 5};
}
6
  • I have few test cases which follows the format like testxyz() ,testpqr() .my existing test class extends TestCase.
    – Giridhar
    Apr 15, 2009 at 17:58
  • 1
    first you must convert them to JUnit 4 (by simply using @Test annotation, in most cases)
    – dfa
    Apr 15, 2009 at 18:27
  • simply great that works.In this case you had suggested me to use certain annotations.How do i know what annotations are there and what to use with respect to junit?
    – Giridhar
    Apr 15, 2009 at 18:39
  • What if I wanted this to act like a set of different tests, so that I may see which data point makes the test fail? (i.e. in your example I would have 4 passing tests and 1 failing, instead of a single failed test) Feb 18, 2010 at 16:38
  • Is it possible to have datapoints of same datatype ? for ex: two datapoints with int[]
    – Sarath
    Jul 8, 2015 at 12:14
25

It sounds like that is a perfect candidate for parametrized tests.

But, basically, parametrized tests allow you to run the same set of tests on different data.

Here are some good blog posts about it:

12

Recently I started zohhak project. It lets you write:

@TestWith({
   "25 USD, 7",
   "38 GBP, 2",
   "null,   0"
})
public void testMethod(Money money, int anotherParameter) {
   ...
}
1
  • 1
    Do note that by default the separator is comma , - if this conflicts with your test data you can change the separator by specifying it explicitly, i.e. @TestWith({"25 USD# 0"}, separator = "#")
    – Robotnik
    Jul 31, 2018 at 2:12
8

A much better way (allows you to have more than one test method) is to use JUnit with JUnitParams:

import junitparams.Parameters;
import org.junit.Test;

@RunWith(JUnitParamsRunner.class)
Public class TestClass() {

    @Test
    @Parameters(method = "generateTestData")
    public void test1(int value, String text, MyObject myObject) {
        ... Test Code ...
    }

    .... Other @Test methods ....

    Object[] generateTestData() {
        MyObject objectA1 = new MyObject();
        MyObject objectA2 = new MyObject();
        MyObject objectA3 = new MyObject();

        return $(
            $(400, "First test text.", objectA1),
            $(402, "Second test text.", objectA2),
            $(403, "Third test text.", objectA3)
        );
    }
}

You can get the junitparams project here.

5

In JUnit 5 you could use @ParameterizedTest and @ValueSource to get a method called multiple times with different input values.

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class PrimeTest {
    @ParameterizedTest
    @ValueSource(ints = {1, 2, 3, 4, 5})
    public void isPrime(int candidate) {
        // called with candidate=1, candidate=2, etc.
    }
}
3

Here is a post I wrote that shows several ways of running the tests repeatedly with code examples:

Run a JUnit test repeatedly

You can use the @Parametrized runner, or use the special runner included in the post

2

I always just make a helper method that executes the test based on the parameters, and then call that method from the JUnit test method. Normally this would mean a single JUnit test method would actually execute lots of tests, but that wasn't a problem for me. If you wanted multiple test methods, one for each distinct invocation, I'd recommend generating the test class.

2

If you are already using a @RunWith in your test class you probably need to take a look at this.

2
  • 2018-01-19 downvote. Please have the courtesy to explain your thinking. You may learn something. Jan 19, 2018 at 19:40
  • That's a pretty arrogant way to ask that, which is generally going to ensure no reply. You'd be much better off saying, "one of us may learn something," as you might well be missing something yourself.
    – TiggerToo
    Jul 9, 2019 at 12:50
1

If you don't want or can't use custom runner (eg. you are already using an other runner, like Robolectric runner), you can try this DataSet Rule.

Your Answer

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

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