View Javadoc
1   package junit.tests.framework;
2   
3   import junit.framework.Test;
4   import junit.framework.TestCase;
5   import junit.framework.TestFailure;
6   import junit.framework.TestResult;
7   import junit.framework.TestSuite;
8   import junit.tests.WasRun;
9   
10  /**
11   * A test case testing the testing framework.
12   */
13  public class TestCaseTest extends TestCase {
14  
15      static class TornDown extends TestCase {
16          boolean fTornDown = false;
17  
18          @Override
19          protected void tearDown() {
20              fTornDown = true;
21          }
22  
23          @Override
24          protected void runTest() {
25              throw new Error("running");
26          }
27      }
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.framework.TestCaseTest)", toString());
34      }
35  
36      public void testError() {
37          TestCase error = new TestCase("error") {
38              @Override
39              protected void runTest() {
40                  throw new Error();
41              }
42          };
43          verifyError(error);
44      }
45  
46      public void testRunAndTearDownFails() {
47          TornDown fails = new TornDown() {
48              @Override
49              protected void tearDown() {
50                  super.tearDown();
51                  throw new Error();
52              }
53  
54              @Override
55              protected void runTest() {
56                  throw new Error();
57              }
58          };
59          verifyError(fails);
60          assertTrue(fails.fTornDown);
61      }
62  
63      public void testSetupFails() {
64          TestCase fails = new TestCase("success") {
65              @Override
66              protected void setUp() {
67                  throw new Error();
68              }
69  
70              @Override
71              protected void runTest() {
72              }
73          };
74          verifyError(fails);
75      }
76  
77      public void testSuccess() {
78          TestCase success = new TestCase("success") {
79              @Override
80              protected void runTest() {
81              }
82          };
83          verifySuccess(success);
84      }
85  
86      public void testFailure() {
87          TestCase failure = new TestCase("failure") {
88              @Override
89              protected void runTest() {
90                  fail();
91              }
92          };
93          verifyFailure(failure);
94      }
95  
96      public void testTearDownAfterError() {
97          TornDown fails = new TornDown();
98          verifyError(fails);
99          assertTrue(fails.fTornDown);
100     }
101 
102     public void testTearDownFails() {
103         TestCase fails = new TestCase("success") {
104             @Override
105             protected void tearDown() {
106                 throw new Error();
107             }
108 
109             @Override
110             protected void runTest() {
111             }
112         };
113         verifyError(fails);
114     }
115 
116     public void testTearDownSetupFails() {
117         TornDown fails = new TornDown() {
118             @Override
119             protected void setUp() {
120                 throw new Error();
121             }
122         };
123         verifyError(fails);
124         assertTrue(!fails.fTornDown);
125     }
126 
127     public void testWasRun() {
128         WasRun test = new WasRun();
129         test.run();
130         assertTrue(test.fWasRun);
131     }
132 
133     public void testExceptionRunningAndTearDown() {
134         // With 1.4, we should
135         // wrap the exception thrown while running with the exception thrown
136         // while tearing down
137         Test t = new TornDown() {
138             @Override
139             public void tearDown() {
140                 throw new Error("tearingDown");
141             }
142         };
143         TestResult result = new TestResult();
144         t.run(result);
145         TestFailure failure = result.errors().nextElement();
146         assertEquals("running", failure.thrownException().getMessage());
147     }
148 
149     public void testErrorTearingDownDoesntMaskErrorRunning() {
150         final Exception running = new Exception("Running");
151         TestCase t = new TestCase() {
152             @Override
153             protected void runTest() throws Throwable {
154                 throw running;
155             }
156 
157             @Override
158             protected void tearDown() throws Exception {
159                 throw new Error("Tearing down");
160             }
161         };
162         try {
163             t.runBare();
164         } catch (Throwable thrown) {
165             assertSame(running, thrown);
166         }
167     }
168 
169     public void testNoArgTestCasePasses() {
170         Test t = new TestSuite(NoArgTestCaseTest.class);
171         TestResult result = new TestResult();
172         t.run(result);
173         assertTrue(result.runCount() == 1);
174         assertTrue(result.failureCount() == 0);
175         assertTrue(result.errorCount() == 0);
176     }
177 
178     public void testNamelessTestCase() {
179         TestCase t = new TestCase() {
180         };
181         TestResult result = t.run();
182         assertEquals(1, result.failureCount());
183     }
184 
185     void verifyError(TestCase test) {
186         TestResult result = test.run();
187         assertTrue(result.runCount() == 1);
188         assertTrue(result.failureCount() == 0);
189         assertTrue(result.errorCount() == 1);
190     }
191 
192     void verifyFailure(TestCase test) {
193         TestResult result = test.run();
194         assertTrue(result.runCount() == 1);
195         assertTrue(result.failureCount() == 1);
196         assertTrue(result.errorCount() == 0);
197     }
198 
199     void verifySuccess(TestCase test) {
200         TestResult result = test.run();
201         assertTrue(result.runCount() == 1);
202         assertTrue(result.failureCount() == 0);
203         assertTrue(result.errorCount() == 0);
204     }
205 }