John Goering
John Goering

Reputation: 39030

How to add a JUnit 4 test that doesn't extend from TestCase to a TestSuite?

In JUnit 3 I simply called

suite.addTestSuite( MyTest.class )

However if MyTest is a JUnit 4 test which does not extend TestCase this doesn't work. What should I do instead to create a suite of tests?

Upvotes: 13

Views: 7202

Answers (2)

Josh
Josh

Reputation: 695

For those with a large set of 3.8 style suites/tests that need to coexist with the new v4 style you can do the following:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
  // Add a JUnit 3 suite
  CalculatorSuite.class,
  // JUnit 4 style tests
  TestCalculatorAddition.class,
  TestCalculatorDivision.class
})
public class CalculatorSuite {
    // A traditional JUnit 3 suite
    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTestSuite(TestCalculatorSubtraction.class);
        return suite;
    }
}

Upvotes: 10

John Goering
John Goering

Reputation: 39030

Found the answer myself: here

Like so:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
  TestCalculatorAddition.class,
  TestCalculatorSubtraction.class,
  TestCalculatorMultiplication.class,
  TestCalculatorDivision.class
})
public class CalculatorSuite {
    // the class remains completely empty, 
    // being used only as a holder for the above annotations
}

Upvotes: 10

Related Questions