KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > samples > SimpleTest


1 package junit.samples;
2
3 import junit.framework.*;
4
5 /**
6  * Some simple tests.
7  *
8  */

9 public class SimpleTest extends TestCase {
10     protected int fValue1;
11     protected int fValue2;
12
13     protected void setUp() {
14         fValue1= 2;
15         fValue2= 3;
16     }
17     public static Test suite() {
18
19         /*
20          * the type safe way
21          *
22         TestSuite suite= new TestSuite();
23         suite.addTest(
24             new SimpleTest("add") {
25                  protected void runTest() { testAdd(); }
26             }
27         );
28
29         suite.addTest(
30             new SimpleTest("testDivideByZero") {
31                  protected void runTest() { testDivideByZero(); }
32             }
33         );
34         return suite;
35         */

36
37         /*
38          * the dynamic way
39          */

40         return new TestSuite(SimpleTest.class);
41     }
42     public void testAdd() {
43         double result= fValue1 + fValue2;
44         // forced failure result == 5
45
assertTrue(result == 6);
46     }
47     public void testDivideByZero() {
48         int zero= 0;
49         int result= 8/zero;
50     }
51     public void testEquals() {
52         assertEquals(12, 12);
53         assertEquals(12L, 12L);
54         assertEquals(new Long JavaDoc(12), new Long JavaDoc(12));
55
56         assertEquals("Size", 12, 13);
57         assertEquals("Capacity", 12.0, 11.99, 0.0);
58     }
59     public static void main (String JavaDoc[] args) {
60         junit.textui.TestRunner.run(suite());
61     }
62 }
Popular Tags