Init class being tested in test case method : TestCase « JUnit « Java Tutorial






import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class TestClassTwo extends TestCase {

  public TestClassTwo(String method) {
    super(method);
  }

  public void testLongRunner() {
    TSP tsp = new TSP();
    assertEquals(2300, tsp.shortestPath(50));
  }

  public void testShortTest() {
    TSP tsp = new TSP();
    assertEquals(140, tsp.shortestPath(5));
  }

  public void testAnotherShortTest() {
    TSP tsp = new TSP();
    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;
  }

}

class TSP {
  public int shortestPath(int numCities) {
    switch (numCities) {
    case 50:
      return 2300;
    case 5:
      return 140;
    case 10:
      return 586;
    }
    return 0;
  }
}








39.2.TestCase
39.2.1.extends TestCase
39.2.2.Use assertEquals in a test method
39.2.3.JUnit Test Composition
39.2.4.Per-Test Setup and Tear-Down
39.2.5.Test Skeleton
39.2.6.Test your stack structure
39.2.7.Test algorithm
39.2.8.Init class being tested in test case method