KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > tests > TestCaseTest


1 package junit.tests;
2
3 import java.util.Vector JavaDoc;
4 import junit.framework.*;
5
6 /**
7  * A test case testing the testing framework.
8  *
9  */

10 public class TestCaseTest extends TestCase {
11     
12     static class TornDown extends TestCase {
13         boolean fTornDown= false;
14         
15         TornDown(String JavaDoc name) {
16             super(name);
17         }
18         protected void tearDown() {
19             fTornDown= true;
20         }
21         protected void runTest() {
22             throw new Error JavaDoc();
23         }
24     }
25
26     public TestCaseTest(String JavaDoc name) {
27         super(name);
28     }
29     public void testCaseToString() {
30         // This test wins the award for twisted snake tail eating while
31
// writing self tests. And you thought those weird anonymous
32
// inner classes were bad...
33
assertEquals("testCaseToString(junit.tests.TestCaseTest)", toString());
34     }
35     public void testError() {
36         TestCase error= new TestCase("error") {
37             protected void runTest() {
38                 throw new Error JavaDoc();
39             }
40         };
41         verifyError(error);
42     }
43     public void testRunAndTearDownFails() {
44         TornDown fails= new TornDown("fails") {
45             protected void tearDown() {
46                 super.tearDown();
47                 throw new Error JavaDoc();
48             }
49             protected void runTest() {
50                 throw new Error JavaDoc();
51             }
52         };
53         verifyError(fails);
54         assertTrue(fails.fTornDown);
55     }
56     public void testSetupFails() {
57         TestCase fails= new TestCase("success") {
58             protected void setUp() {
59                 throw new Error JavaDoc();
60             }
61             protected void runTest() {
62             }
63         };
64         verifyError(fails);
65     }
66     public void testSuccess() {
67         TestCase success= new TestCase("success") {
68             protected void runTest() {
69             }
70         };
71         verifySuccess(success);
72     }
73     public void testFailure() {
74         TestCase failure= new TestCase("failure") {
75             protected void runTest() {
76                 fail();
77             }
78         };
79         verifyFailure(failure);
80     }
81
82     public void testTearDownAfterError() {
83         TornDown fails= new TornDown("fails");
84         verifyError(fails);
85         assertTrue(fails.fTornDown);
86     }
87     
88     public void testTearDownFails() {
89         TestCase fails= new TestCase("success") {
90             protected void tearDown() {
91                 throw new Error JavaDoc();
92             }
93             protected void runTest() {
94             }
95         };
96         verifyError(fails);
97     }
98     public void testTearDownSetupFails() {
99         TornDown fails= new TornDown("fails") {
100             protected void setUp() {
101                 throw new Error JavaDoc();
102             }
103         };
104         verifyError(fails);
105         assertTrue(!fails.fTornDown);
106     }
107     public void testWasRun() {
108         WasRun test= new WasRun("");
109         test.run();
110         assertTrue(test.fWasRun);
111     }
112     void verifyError(TestCase test) {
113         TestResult result= test.run();
114         assertTrue(result.runCount() == 1);
115         assertTrue(result.failureCount() == 0);
116         assertTrue(result.errorCount() == 1);
117     }
118     void verifyFailure(TestCase test) {
119         TestResult result= test.run();
120         assertTrue(result.runCount() == 1);
121         assertTrue(result.failureCount() == 1);
122         assertTrue(result.errorCount() == 0);
123     }
124     void verifySuccess(TestCase test) {
125         TestResult result= test.run();
126         assertTrue(result.runCount() == 1);
127         assertTrue(result.failureCount() == 0);
128         assertTrue(result.errorCount() == 0);
129     }
130
131
132 }
Popular Tags