JUnit错误 - IllegalArgumentException:Test类只能有一个构造函数

时间:2012-02-23 14:49:40

标签: java junit

单元测试对我来说是新的,我有这个错误,我不明白。 我有2个TestCases子类在独立运行时工作正常,但不在我的测试套件中。

在测试套件(下面的AllTest类)中,前3个工作正常,但AvailableResouresTest和ModelTest会生成错误。

我怀疑这与我必须在AllTest中导入这两个类(并且只有它们)这一事实有关,而它们都位于同一个包中。

我使用Eclipse向导创建了这两个测试用例。但是,我在代码中找不到明显的差异,因此可能会有些新鲜的眼睛可以提供帮助。非常感谢你

测试套件代码:

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";
}

非工作测试:

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");
}
}

堆栈跟踪:

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)

1 个答案:

答案 0 :(得分:5)

如果您使用的是JUnit4,那么您的类不应该扩展TestCase,您只需要在测试方法之前使用注释@Test

你的测试套件应该像这样开始:

@RunWith(Suite.class)
@SuiteClasses({TaskTest.class, 
                ResourceTest.class,
                DateForTMSTest.class,
                AvailableResources.class,
                Model.class})