Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test class can only have one constructor

Anybody encountered this error

initializationError(junit.framework.TestSuite): Test class can only have one constructor

Use Case.

I have 2 TestScripts in a TestSuite. If I dynamically create the TestSuite and then add the 2 testScripts and invoke this

Result result = JUnitCore.runClasses(clazz);

where clazz is the TestSuite class.

However I if invoke the JUnitCore.runClasses on each 2 TestScripts there is no problem.

I used JUnit3.

I have similar question with this JUnit error - IllegalArgumentException:Test class can only have one constructor, but mine involves creating the test suite class dynamically. Reason being is that there is a usecase that I could just include 1 testscript in a testsuite or i could have 2 testscripts in a testsuite.

UPDATE code:

public class ScriptTest1 extends TestCase { 
    private ScriptTest1() { }   
    public void testMethod1() {
        Assert.assertEquals(true, true);
    }
}

public class ScriptTest2 extends TestCase { 
    private ScriptTest2() { }   
    public void testMethod2() {
        Assert.assertEquals(true, true);
    }
}

Then in another class

File file = new File("file to ScriptTest.class");
Class<?> clazz = Class.forName(className, true, Thread.currentThread().getContextClassLoader());

//construct a new test suite
TestSuite ts = new TestSuite("Sample Test Suite with only ScriptTest1");
ts.addTestSuite((Class<? extends TestCase>) clazz);

Result result = JUnitCore.runClasses(ts.getClass());
HashSet<String> failureMethod = new HashSet<String>();
for (Failure failure : result.getFailures()) {
     System.out.println("Failure: " + failure.toString());      
}

Failure will output: initializationError(junit.framework.TestSuite): Test class can only have one constructor However if I just directly do this, it will run fine.

File file = new File("file to ScriptTest.class");
Class<?> clazz = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
Result result = JUnitCore.runClasses(clazz);

I created the test classes in JUnit4 and Im running my main program with dependency on JUnit4 also.

like image 280
jantox Avatar asked Nov 13 '22 01:11

jantox


1 Answers

You're passing the class junit.framework.TestSuite.class to JUnitCore.runClasses(). JUnitCore.runClasses tries to instantiate the class. To avoid problems, it requires that any class that you pass has only one constructor [*]. TestSuite has multiple public constructors.

Try:

TestResult result = new TestResult();
ts.run(result);

for (Enumeration<TestFailure> failures = result.failures(); failures.hasMoreElements();) {
    TestFailure testFailure = failures.nextElement();
    System.out.println("Failure: " + testFailure.toString());      
}

In the meantime, think about upgradng your test cases from JUnit 3 to JUnit 4, JUnit 3 is getting quite old now.

[*] Except for Parameterized tests, etc. In the OP's use case, it's true.

like image 132
Matthew Farwell Avatar answered Dec 24 '22 11:12

Matthew Farwell