Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized test case classes in JUnit 3.x

Tags:

java

junit

I have a JUnit 3.x TestCase which I would like to be able to parameterize. I'd like to parametrize the entire TestCase (including the fixture). However, the TestSuite.addTestSuite() method does not allow be to pass a TestCase object, just a class:

   TestSuite suite = new TestSuite("suite");
   suite.addTestSuite(MyTestCase.class);

I would like to be able to pass a parameter (a string) to the MyTestCase instance which is created when the test runs. As it is now, I have to have a separate class for each parameter value.

I tried passing it an anynomous subclass:

   MyTestCase testCase = new MyTestCase() {
       String getOption() {
           return "some value";
       }
   }

   suite.addTestSuite(testCase.getClass());

However, this fails with the assertion:

   ... MyTestSuite$1 has no public constructor TestCase(String name) or TestCase()`

Any ideas? Am I attacking the problem the wrong way?

like image 580
JesperE Avatar asked Oct 13 '08 12:10

JesperE


1 Answers

If this is Java 5 or higher, you might want to consider switching to JUnit 4, which has support for parameterized test cases built in.

like image 179
Powerlord Avatar answered Sep 28 '22 10:09

Powerlord