View Javadoc
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 testAssertionFailedErrorToStringWithNoMessage() {
30          // Also, we are testing fail, so we can't rely on fail() working.
31          // We have to throw the exception manually.
32          try {
33              fail();
34          } catch (AssertionFailedError e) {
35              assertEquals("junit.framework.AssertionFailedError", e.toString());
36              return;
37          }
38          throw new AssertionFailedError();
39      }
40  
41      public void testAssertionFailedErrorToStringWithMessage() {
42          // Also, we are testing fail, so we can't rely on fail() working.
43          // We have to throw the exception manually.
44          try {
45              fail("woops!");
46          } catch (AssertionFailedError e) {
47              assertEquals("junit.framework.AssertionFailedError: woops!", e.toString());
48              return;
49          }
50          throw new AssertionFailedError();
51      }
52  
53      public void testAssertEquals() {
54          Object o = new Object();
55          assertEquals(o, o);
56          try {
57              assertEquals(new Object(), new Object());
58          } catch (AssertionFailedError e) {
59              return;
60          }
61          fail();
62      }
63  
64      public void testAssertEqualsNull() {
65          assertEquals((Object) null, (Object) null);
66      }
67  
68      public void testAssertStringEquals() {
69          assertEquals("a", "a");
70      }
71  
72      public void testAssertNullNotEqualsString() {
73          try {
74              assertEquals(null, "foo");
75              fail();
76          } catch (ComparisonFailure e) {
77          }
78      }
79  
80      public void testAssertStringNotEqualsNull() {
81          try {
82              assertEquals("foo", null);
83              fail();
84          } catch (ComparisonFailure e) {
85              e.getMessage(); // why no assertion?
86          }
87      }
88  
89      public void testAssertNullNotEqualsNull() {
90          try {
91              assertEquals(null, new Object());
92          } catch (AssertionFailedError e) {
93              e.getMessage(); // why no assertion?
94              return;
95          }
96          fail();
97      }
98  
99      public void testAssertNull() {
100         assertNull(null);
101         try {
102             assertNull(new Object());
103         } catch (AssertionFailedError e) {
104             return;
105         }
106         fail();
107     }
108 
109     public void testAssertNotNull() {
110         assertNotNull(new Object());
111         try {
112             assertNotNull(null);
113         } catch (AssertionFailedError e) {
114             return;
115         }
116         fail();
117     }
118 
119     public void testAssertTrue() {
120         assertTrue(true);
121         try {
122             assertTrue(false);
123         } catch (AssertionFailedError e) {
124             return;
125         }
126         fail();
127     }
128 
129     public void testAssertFalse() {
130         assertFalse(false);
131         try {
132             assertFalse(true);
133         } catch (AssertionFailedError e) {
134             return;
135         }
136         fail();
137     }
138 
139     public void testAssertSame() {
140         Object o = new Object();
141         assertSame(o, o);
142         try {
143             assertSame(new Integer(1), new Integer(1));
144         } catch (AssertionFailedError e) {
145             return;
146         }
147         fail();
148     }
149 
150     public void testAssertNotSame() {
151         assertNotSame(new Integer(1), null);
152         assertNotSame(null, new Integer(1));
153         assertNotSame(new Integer(1), new Integer(1));
154         try {
155             Integer obj = new Integer(1);
156             assertNotSame(obj, obj);
157         } catch (AssertionFailedError e) {
158             return;
159         }
160         fail();
161     }
162 
163     public void testAssertNotSameFailsNull() {
164         try {
165             assertNotSame(null, null);
166         } catch (AssertionFailedError e) {
167             return;
168         }
169         fail();
170     }
171 }