Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

junit

junit3

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?

like image 770
Epaga Avatar asked Oct 17 '08 08:10

Epaga


People also ask

How do you ignore test cases in JUnit 4?

JUnit 4 – @Ignore Annotation The JUnit 4 @Ignore annotation could be applied for a test method, to skip its execution. In this case, you need to use @Ignore with the @Test annotation for a test method you wish to skip. The annotation could also be applied to the test class, to skip all the test cases under a class.

How do I run a JUnit test from another class?

Create Test Runner Class It imports the JUnitCore class and uses the runClasses() method that takes the test class name as its parameter. Compile the Test case and Test Runner classes using javac. Now run the Test Runner, which will run the test case defined in the provided Test Case class. Verify the output.

What is not necessary to place in classpath during the execution of JUnit test cases from ant?

Do not put either in ANT_HOME/lib , and instead include their locations in your CLASSPATH environment variable. Add both JARs to your classpath using -lib . Specify the locations of both JARs using a <classpath> element in a <taskdef> in the build file. Leave ant-junit.

How do we run setUp () and tearDown () methods once for all the tests in the class for JUnit framework?

As outlined in Recipe 4.6, JUnit calls setUp( ) before each test, and tearDown( ) after each test. In some cases you might want to call a special setup method once before a series of tests, and then call a teardown method once after all tests are complete. The junit. extensions.


1 Answers

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;
    }
}
like image 75
Josh Avatar answered Oct 14 '22 09:10

Josh