Issue
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.
Solution
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
}
}
Answered By - dkatzel
Answer Checked By - Pedro (JavaFixing Volunteer)