KickJava   Java API By Example, From Geeks To Geeks.

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


1 package junit.tests.framework;
2
3 import junit.framework.AssertionFailedError;
4 import junit.framework.ComparisonFailure;
5 import junit.framework.TestCase;
6
7 public class AssertTest extends TestCase {
8
9     /* In the tests that follow, we can't use standard formatting
10      * for exception tests:
11      * try {
12      * somethingThatShouldThrow();
13      * fail();
14      * catch (AssertionFailedError e) {
15      * }
16      * because fail() would never be reported.
17      */

18     public void testFail() {
19         // Also, we are testing fail, so we can't rely on fail() working.
20
// We have to throw the exception manually, .
21
try {
22             fail();
23         } catch (AssertionFailedError e) {
24             return;
25         }
26         throw new AssertionFailedError();
27     }
28
29     public void testAssertEquals() {
30         Object JavaDoc o= new Object JavaDoc();
31         assertEquals(o, o);
32         try {
33             assertEquals(new Object JavaDoc(), new Object JavaDoc());
34         } catch (AssertionFailedError e) {
35             return;
36         }
37         fail();
38     }
39
40     public void testAssertEqualsNull() {
41         assertEquals(null, null);
42     }
43
44     public void testAssertStringEquals() {
45         assertEquals("a", "a");
46     }
47
48     public void testAssertNullNotEqualsString() {
49         try {
50             assertEquals(null, "foo");
51             fail();
52         } catch (ComparisonFailure e) {
53         }
54     }
55
56     public void testAssertStringNotEqualsNull() {
57         try {
58             assertEquals("foo", null);
59             fail();
60         } catch (ComparisonFailure e) {
61             e.getMessage(); // why no assertion?
62
}
63     }
64
65     public void testAssertNullNotEqualsNull() {
66         try {
67             assertEquals(null, new Object JavaDoc());
68         } catch (AssertionFailedError e) {
69             e.getMessage(); // why no assertion?
70
return;
71         }
72         fail();
73     }
74
75     public void testAssertNull() {
76         assertNull(null);
77         try {
78             assertNull(new Object JavaDoc());
79         } catch (AssertionFailedError e) {
80             return;
81         }
82         fail();
83     }
84
85     public void testAssertNotNull() {
86         assertNotNull(new Object JavaDoc());
87         try {
88             assertNotNull(null);
89         } catch (AssertionFailedError e) {
90             return;
91         }
92         fail();
93     }
94
95     public void testAssertTrue() {
96         assertTrue(true);
97         try {
98             assertTrue(false);
99         } catch (AssertionFailedError e) {
100             return;
101         }
102         fail();
103     }
104
105     public void testAssertFalse() {
106         assertFalse(false);
107         try {
108             assertFalse(true);
109         } catch (AssertionFailedError e) {
110             return;
111         }
112         fail();
113     }
114
115     public void testAssertSame() {
116         Object JavaDoc o= new Object JavaDoc();
117         assertSame(o, o);
118         try {
119             assertSame(new Integer JavaDoc(1), new Integer JavaDoc(1));
120         } catch (AssertionFailedError e) {
121             return;
122         }
123         fail();
124     }
125
126     public void testAssertNotSame() {
127         assertNotSame(new Integer JavaDoc(1), null);
128         assertNotSame(null, new Integer JavaDoc(1));
129         assertNotSame(new Integer JavaDoc(1), new Integer JavaDoc(1));
130         try {
131             Integer JavaDoc obj= new Integer JavaDoc(1);
132             assertNotSame(obj, obj);
133         } catch (AssertionFailedError e) {
134             return;
135         }
136         fail();
137     }
138
139     public void testAssertNotSameFailsNull() {
140         try {
141             assertNotSame(null, null);
142         } catch (AssertionFailedError e) {
143             return;
144         }
145         fail();
146     }
147 }
Popular Tags