运行在JUnit中的TestSuite上添加的测试

时间:2014-05-05 16:31:53

标签: java unit-testing junit

我正在阅读Pragmatic Unit Testing in Java with JUnit,在第3.4部分,它提供了根据本书应该运行的代码。我很困惑它显然无法将TestSuite作为测试返回,这是对本书的修正吗?应该怎样做才能做到这一点? (只进行短测试)

import junit.framework.*;
public class TestClassTwo extends TestCase {
    public TestClassTwo(String method) {
        super(method);
    }

    // This one takes a few hours...
    public void testLongRunner() {
         TSP tsp = new TSP(); // Loard with default cities
         assertEquals(140, tsp.shortestPath(5)); // top 5
    }

    public void testAnotherShortTest() {
        TSP tsp = new TSP(); // Load with default cities
        assertEquals(586, tsp.shortestPath(10));
    }
    public static Test suite() {
         TestSuite suite = new TestSuite();
         // Only include short tests
         suite.addTest( new TestClassTwo("testShortTest"));
         suite.addTest(new TestClassTwo("testAnotherShortTest"));
         return suite;
    }
}

所以我尝试运行它,这里是堆栈跟踪,它也给出了一个initializationError。如果我添加一个强制转换它仍然给我一个initializationError。

  java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from TestSuite to Test
    at PracticeTest.suite(PracticeTest.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.internal.runners.SuiteMethod.testFromSuiteMethod(SuiteMethod.java:35)
    at org.junit.internal.runners.SuiteMethod.<init>(SuiteMethod.java:24)
    at org.junit.internal.builders.SuiteMethodBuilder.runnerForClass(SuiteMethodBuilder.java:11)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
    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 :(得分:1)

本书中的示例似乎有点陈旧,因为它在JUnit 3.x风格中。它没错,请注意 - TestSuite实现了Test界面,所以它可以正常工作。

但是,在现代JUnit 4.x测试中,将测试聚合到套件的推荐方法是using the @Suite annotation