Michael
Michael

Reputation: 42050

How to write dynamic Test Case

Suppose, I have a junit test class:

class MyComponentTest {

  private void test(File file) {...}

  @Test public void test1() {test("test1.txt")}
  @Test public void test2() {test("test2.txt")}
  @Test public void test3() {test("test3.txt")}
}

The test method reads the input data from the file and test the component with the input data.

What if I am changing the MyComponentTest ?

class MyComponentTest {

  private void test(File file) {...}

  @Test public void testAll() {
    for (String name: dir.list())
      test(new File(name))
  }
}

Now I have only one test (testAll) instead of three tests (test1, test2, and test3) as it was before. The Runner for example will not recognize three separate tests as it did in the previous version of the class.

My question is: how to make each test method invocation a separate test from the junit point of view ?

Upvotes: 4

Views: 1964

Answers (1)

Aravind Yarram
Aravind Yarram

Reputation: 80176

Though you can use Junit's Parameterized tests for this it is bit involved and ugly. I suggest you looking at the spockframework which simplifies this a lot. And there is also another option in TestNG.

Upvotes: 2

Related Questions