In previous tutorial we introduced how to run a single test case, however, most of the time we have to bunch same kind of cases along, this is what “suite test” stands for in Junit, a suite is a collection of test cases that are intended to be used to test a software program to show that it has some specified set of behaviors. In Junit, annotation @RunWith and @Suite are used to run a suite test.

Using junit4 annotation for Suite Test

@RunWith(Suite.class)
@Suite.SuiteClasses({
  Test1.class,
  Test2.class,
  Test3.class,

  TestSuite2.class
})
public class TestSuite1 {
}

Using Suite Test similar to junit 3

If you only want run some cases in a test class, you can invoke the suite method into your test class.

public static Test suite() {
  TestSuite suite=new TestSuite();
  //only allow to run test method testGetInt
  suite.addTest(new Equals1Test("testGetInt"));
  return suite;
}

Also, you can invoke some test methods in other class, just like the following it also will run the specified method in test class Equals1Test.

public static Test suite()
{
  TestSuite suite=new TestSuite();
  suite.addTestSuite(TestTest1.class);
  //invoke the test method
  suite.addTest(Equals1Test.suite());
  return suite;
}

Actually above code run in junit3, if you want to immigrate junit 4, we need to use JUnit4TestAdapter to convert.

public static Test suite() {
  TestSuite suite=new TestSuite();
  suite.addTest(new JUnit4TestAdapter(Test1.class));
  suite.addTest(new JUnit4TestAdapter(Test2.class));
  return suite;
}