Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit error - IllegalArgumentException:Test class can only have one constructor

Tags:

java

junit

Unit testing is new to me, and I have this error I don't understand. I have 2 TestCases subclasses that work fine when ran standalone, but not in my test suite.

In the test suite (AllTest class below), the first 3 work fine, but AvailableResouresTest and ModelTest generate the error.

I suspect it has to do with the fact that I had to import those 2 classes (and only them) in AllTest whereas they are all located in the same package.

I used the Eclipse wizard to create those two test cases. However, I can't find noticeable differences in the code, so may be some fresh eye can help. Thank you very much

The test suite code:

package com.tms.client.tests;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import com.tms.client.model.AvailableResources;  // I have to import those 2 classes only
import com.tms.client.model.Model;               // but all my tests are in the same folder

@RunWith(Suite.class)
@SuiteClasses(value = { TaskTest.class, 
                    ResourceTest.class,
                    DateForTMSTest.class,
                    AvailableResources.class,
                    Model.class})
public class AllTests{
public static final String SCHEME_FILENAME= "config/project_schema.inc2.xsd";
public static final String RESOURCE_FILENAME = "config/resourceList.inc2.txt";
public static final String PROJECT_FILENAME = "input/project.inc2.e.xml";
public static final String PROJECT_FILENAME_SAVE_LOCATION =     "input/project.save.tmp.xml";
}

The non working test:

package com.tms.client.tests;
import java.util.ArrayList;
import java.util.Hashtable;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import com.tms.client.model.AvailableResources;
import com.tms.client.model.Model;
import com.tms.client.model.ParseXML;
import com.tms.client.model.Task;

public class AvailableResourcesTest extends TestCase{

ArrayList<String> stringsFromFile;
Class<AvailableResources> dummyAR;
Model model;

@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
    super.setUp();
    ParseXML.setSchemaFile(AllTests.SCHEME_FILENAME);
    model = new Model(AllTests.RESOURCE_FILENAME, AllTests.PROJECT_FILENAME);
    dummyAR = AvailableResources.class;
    stringsFromFile = Helper.getLinesFromFile(AllTests.RESOURCE_FILENAME);
}

@SuppressWarnings("unchecked")
@Test
public void testFlushAllResources() {
    //Get the size BEFORE flushing
    Hashtable<Integer,Task> mapIdToObject = (Hashtable<Integer,Task>) PrivateAccessor.getPrivateFieldStatic(dummyAR, "mapIDtoName");
    int sizeBeforeFlush = mapIdToObject.size();
    // Flush tasks
    PrivateAccessor.invokePrivateMethodStatic(dummyAR, "flushAllResources", null);
    // Get the size AFTER flushing
    mapIdToObject = (Hashtable<Integer,Task>) PrivateAccessor.getPrivateFieldStatic(dummyAR, "mapIDtoName");
    int sizeAfterFlush = mapIdToObject.size();
    boolean test = sizeBeforeFlush > 0 && sizeAfterFlush == 0;  
    // Add code to remove task
    assertTrue(test);       
}

/**
 * Compare # lines in resource file with mapIdToName.size()
 */
@Test
public void testSize() {
    Hashtable<Integer,Task> mapIdToName = (Hashtable<Integer,Task>) PrivateAccessor.getPrivateFieldStatic(dummyAR, "mapIDtoName");
    assertEquals(mapIdToName.size(), stringsFromFile.size());
}

/**
 * Check that every resource id from file is available in model
 */
@Test
public void testIsAvailable() {
    for(String s: stringsFromFile){
        int resourceId = Integer.parseInt(s.split(";")[0]); // the resourceId from file
        assertTrue(AvailableResources.isAvailable(resourceId));
    }
}

/**
 * Check that the name returned corresponds to the name in file
 */
@Test
public void testGetNameFromID() {
    for(String s: stringsFromFile){
        int resourceId = Integer.parseInt(s.split(";")[0]); // the resourceId from file
        String resourceName = s.split(";")[1]; // the resourceId from file
        assertEquals(AvailableResources.getNameFromID(resourceId), resourceName);
    }
}

@Test
public void testAddNameByID() {
    fail("Not yet implemented");

}

@Test
public void testRemoveResourceByID() {
    fail("Not yet implemented");
}
}

And the stack trace:

java.lang.IllegalArgumentException: Test class can only have one constructor
at org.junit.runners.model.TestClass.<init>(TestClass.java:37)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:55)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:98)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:84)
at org.junit.runners.Suite.<init>(Suite.java:101)
at org.junit.runners.Suite.<init>(Suite.java:67)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:35)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
like image 209
znat Avatar asked Feb 23 '12 14:02

znat


People also ask

Can you only have one constructor per class?

Can a Class have more than one Constructor? Yes, a Class in ABL can have more than Constructor. Multiple instance constructors can be defined for a class that are overloaded with different parameter signatures.

Can a test class have a constructor Java?

java - Test class can only have one constructor - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.


1 Answers

If you are using JUnit4 your classes shouldn't extend TestCase, you just need the annotation @Test before your test methods.

And your test suite should begin like this:

@RunWith(Suite.class)
@SuiteClasses({TaskTest.class, 
                ResourceTest.class,
                DateForTMSTest.class,
                AvailableResources.class,
                Model.class})
like image 115
talnicolas Avatar answered Oct 20 '22 00:10

talnicolas