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?
-
6By 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 02 '14 at 18:12
9 Answers
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};
}

- 114,442
- 31
- 189
- 228
-
I have few test cases which follows the format like testxyz() ,testpqr() .my existing test class extends TestCase. – Giridhar Apr 15 '09 at 17:58
-
1first you must convert them to JUnit 4 (by simply using @Test annotation, in most cases) – dfa Apr 15 '09 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 '09 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) – Manrico Corazzi Feb 18 '10 at 16:38
-
Is it possible to have datapoints of same datatype ? for ex: two datapoints with int[] – Sarath Jul 08 '15 at 12:14
-
@ManricoCorazzi i want to implement the same use case i.e. 4 passing test and 1 fail have u got any solution?? – Rahul Jan 02 '18 at 11:15
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:
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) {
...
}
-
1Do 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 '18 at 02:12
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.

- 6,622
- 13
- 56
- 70
Here is a post I wrote that shows several ways of running the tests repeatedly with code examples:
You can use the @Parametrized runner, or use the special runner included in the post
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.
}
}

- 1,257
- 13
- 18
If you are already using a @RunWith
in your test class you probably need to take a look at this.

- 1
- 1

- 14,126
- 11
- 103
- 157
-
2018-01-19 downvote. Please have the courtesy to explain your thinking. You may learn something. – mike rodent Jan 19 '18 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 09 '19 at 12:50
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.

- 13,822
- 6
- 44
- 64
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.

- 2,024
- 1
- 23
- 31