jantristanmilan
jantristanmilan

Reputation: 4358

Running the tests added on a TestSuite in JUnit

I'm reading Pragmatic Unit Testing in Java with JUnit and on part 3.4 it gives this code which according to the book should run. I'm confused it obviously cannot return TestSuite as a Test, is this a correction on the book? What should be the proper way to do this? (Only running the short tests)

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

So I tried running it and here's the stack trace it also gives an initializationError. If I add a cast it still gives me an 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)

Upvotes: 0

Views: 895

Answers (1)

Mureinik
Mureinik

Reputation: 311308

The example in this book seems to be a bit old, as it's in JUnit 3.x flavor. It's not wrong, mind you - TestSuite implements the Test interface, so it would work just fine.

However, in modern JUnit 4.x tests, the recommended way of aggregating tests in to suites is by using the @Suite annotation.

Upvotes: 1

Related Questions