View Javadoc
1   package junit.samples;
2   
3   import junit.framework.Test;
4   import junit.framework.TestCase;
5   import junit.framework.TestSuite;
6   
7   /**
8    * Some simple tests.
9    */
10  public class SimpleTest extends TestCase {
11      protected int fValue1;
12      protected int fValue2;
13  
14      @Override
15      protected void setUp() {
16          fValue1 = 2;
17          fValue2 = 3;
18      }
19  
20      public static Test suite() {
21  
22          /*
23             * the type safe way
24             *
25            TestSuite suite= new TestSuite();
26            suite.addTest(
27                new SimpleTest("add") {
28                     protected void runTest() { testAdd(); }
29                }
30            );
31  
32            suite.addTest(
33                new SimpleTest("testDivideByZero") {
34                     protected void runTest() { testDivideByZero(); }
35                }
36            );
37            return suite;
38            */
39  
40          /*
41             * the dynamic way
42             */
43          return new TestSuite(SimpleTest.class);
44      }
45  
46      public void testAdd() {
47          double result = fValue1 + fValue2;
48          // forced failure result == 5
49          assertTrue(result == 6);
50      }
51  
52      public int unused;
53  
54      public void testDivideByZero() {
55          int zero = 0;
56          int result = 8 / zero;
57          unused = result; // avoid warning for not using result
58      }
59  
60      public void testEquals() {
61          assertEquals(12, 12);
62          assertEquals(12L, 12L);
63          assertEquals(new Long(12), new Long(12));
64  
65          assertEquals("Size", 12, 13);
66          assertEquals("Capacity", 12.0, 11.99, 0.0);
67      }
68  
69      public static void main(String[] args) {
70          junit.textui.TestRunner.run(suite());
71      }
72  }