KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > tests > framework > TestCaseTest


1 package junit.tests.framework;
2
3 import junit.framework.*;
4 import junit.tests.WasRun;
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         protected void tearDown() {
16             fTornDown= true;
17         }
18         protected void runTest() {
19             throw new Error JavaDoc();
20         }
21     }
22
23     public void testCaseToString() {
24         // This test wins the award for twisted snake tail eating while
25
// writing self tests. And you thought those weird anonymous
26
// inner classes were bad...
27
assertEquals("testCaseToString(junit.tests.framework.TestCaseTest)", toString());
28     }
29     public void testError() {
30         TestCase error= new TestCase("error") {
31             protected void runTest() {
32                 throw new Error JavaDoc();
33             }
34         };
35         verifyError(error);
36     }
37     public void testRunAndTearDownFails() {
38         TornDown fails= new TornDown() {
39             protected void tearDown() {
40                 super.tearDown();
41                 throw new Error JavaDoc();
42             }
43             protected void runTest() {
44                 throw new Error JavaDoc();
45             }
46         };
47         verifyError(fails);
48         assertTrue(fails.fTornDown);
49     }
50     public void testSetupFails() {
51         TestCase fails= new TestCase("success") {
52             protected void setUp() {
53                 throw new Error JavaDoc();
54             }
55             protected void runTest() {
56             }
57         };
58         verifyError(fails);
59     }
60     public void testSuccess() {
61         TestCase success= new TestCase("success") {
62             protected void runTest() {
63             }
64         };
65         verifySuccess(success);
66     }
67     public void testFailure() {
68         TestCase failure= new TestCase("failure") {
69             protected void runTest() {
70                 fail();
71             }
72         };
73         verifyFailure(failure);
74     }
75
76     public void testTearDownAfterError() {
77         TornDown fails= new TornDown();
78         verifyError(fails);
79         assertTrue(fails.fTornDown);
80     }
81     
82     public void testTearDownFails() {
83         TestCase fails= new TestCase("success") {
84             protected void tearDown() {
85                 throw new Error JavaDoc();
86             }
87             protected void runTest() {
88             }
89         };
90         verifyError(fails);
91     }
92     public void testTearDownSetupFails() {
93         TornDown fails= new TornDown() {
94             protected void setUp() {
95                 throw new Error JavaDoc();
96             }
97         };
98         verifyError(fails);
99         assertTrue(!fails.fTornDown);
100     }
101     public void testWasRun() {
102         WasRun test= new WasRun();
103         test.run();
104         assertTrue(test.fWasRun);
105     }
106     public void testExceptionRunningAndTearDown() {
107         // This test documents the current behavior. With 1.4, we should
108
// wrap the exception thrown while running with the exception thrown
109
// while tearing down
110
Test t= new TornDown() {
111             public void tearDown() {
112                 throw new Error JavaDoc("tearDown");
113             }
114         };
115         TestResult result= new TestResult();
116         t.run(result);
117         TestFailure failure= (TestFailure) result.errors().nextElement();
118         assertEquals("tearDown", failure.thrownException().getMessage());
119     }
120     
121     public void testNoArgTestCasePasses() {
122         Test t= new TestSuite(NoArgTestCaseTest.class);
123         TestResult result= new TestResult();
124         t.run(result);
125         assertTrue(result.runCount() == 1);
126         assertTrue(result.failureCount() == 0);
127         assertTrue(result.errorCount() == 0);
128     }
129     
130     public void testNamelessTestCase() {
131         TestCase t= new TestCase() {};
132         try {
133             t.run();
134             fail();
135         } catch (AssertionFailedError e) {
136         }
137     }
138     
139     void verifyError(TestCase test) {
140         TestResult result= test.run();
141         assertTrue(result.runCount() == 1);
142         assertTrue(result.failureCount() == 0);
143         assertTrue(result.errorCount() == 1);
144     }
145     void verifyFailure(TestCase test) {
146         TestResult result= test.run();
147         assertTrue(result.runCount() == 1);
148         assertTrue(result.failureCount() == 1);
149         assertTrue(result.errorCount() == 0);
150     }
151     void verifySuccess(TestCase test) {
152         TestResult result= test.run();
153         assertTrue(result.runCount() == 1);
154         assertTrue(result.failureCount() == 0);
155         assertTrue(result.errorCount() == 0);
156     }
157 }
Popular Tags