KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > tests > ExceptionTestCaseTest


1 package junit.tests;
2
3 import junit.framework.*;
4 import junit.extensions.*;
5
6 public class ExceptionTestCaseTest extends junit.framework.TestCase {
7
8     static public class ThrowExceptionTestCase extends ExceptionTestCase {
9         public ThrowExceptionTestCase(String JavaDoc name, Class JavaDoc exception) {
10             super(name, exception);
11         }
12         public void test() {
13             throw new IndexOutOfBoundsException JavaDoc();
14         }
15     }
16
17     static public class ThrowRuntimeExceptionTestCase extends ExceptionTestCase {
18         public ThrowRuntimeExceptionTestCase(String JavaDoc name, Class JavaDoc exception) {
19             super(name, exception);
20         }
21         public void test() {
22             throw new RuntimeException JavaDoc();
23         }
24     }
25
26     static public class ThrowNoExceptionTestCase extends ExceptionTestCase {
27         public ThrowNoExceptionTestCase(String JavaDoc name, Class JavaDoc exception) {
28             super(name, exception);
29         }
30         public void test() {
31         }
32     }
33
34     public ExceptionTestCaseTest(String JavaDoc name) {
35         super(name);
36     }
37     public void testExceptionSubclass() {
38         ExceptionTestCase test= new ThrowExceptionTestCase("test", IndexOutOfBoundsException JavaDoc.class);
39         TestResult result= test.run();
40         assertEquals(1, result.runCount());
41         assertTrue(result.wasSuccessful());
42     }
43     public void testExceptionTest() {
44         ExceptionTestCase test= new ThrowExceptionTestCase("test", IndexOutOfBoundsException JavaDoc.class);
45         TestResult result= test.run();
46         assertEquals(1, result.runCount());
47         assertTrue(result.wasSuccessful());
48     }
49     public void testFailure() {
50         ExceptionTestCase test= new ThrowRuntimeExceptionTestCase("test", IndexOutOfBoundsException JavaDoc.class);
51         TestResult result= test.run();
52         assertEquals(1, result.runCount());
53         assertEquals(1, result.errorCount());
54     }
55     public void testNoException() {
56         ExceptionTestCase test= new ThrowNoExceptionTestCase("test", Exception JavaDoc.class);
57         TestResult result= test.run();
58         assertEquals(1, result.runCount());
59         assertEquals(1, result.failureCount());
60     }
61     public void testWrongException() {
62         ExceptionTestCase test= new ThrowRuntimeExceptionTestCase("test", IndexOutOfBoundsException JavaDoc.class);
63         TestResult result= test.run();
64         assertEquals(1, result.runCount());
65         assertEquals(1, result.errorCount());
66     }
67 }
Popular Tags