Source of AssertTest.java


  1: package junit.tests.framework;

  3: import junit.framework.AssertionFailedError;
  4: import junit.framework.ComparisonFailure;
  5: import junit.framework.TestCase;

  7: public class AssertTest extends TestCase {

  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:         }

 29:         public void testAssertEquals() {
 30:                 Object o= new Object();
 31:                 assertEquals(o, o);
 32:                 try {
 33:                         assertEquals(new Object(), new Object());
 34:                 } catch (AssertionFailedError e) {
 35:                         return;
 36:                 }
 37:                 fail();
 38:         }

 40:         public void testAssertEqualsNull() {
 41:                 assertEquals(null, null);
 42:         }

 44:         public void testAssertStringEquals() {
 45:                 assertEquals("a", "a");
 46:         }

 48:         public void testAssertNullNotEqualsString() {
 49:                 try {
 50:                         assertEquals(null, "foo");
 51:                         fail();
 52:                 } catch (ComparisonFailure e) {
 53:                 }
 54:         }

 56:         public void testAssertStringNotEqualsNull() {
 57:                 try {
 58:                         assertEquals("foo", null);
 59:                         fail();
 60:                 } catch (ComparisonFailure e) {
 61:                         e.getMessage(); // why no assertion?
 62:                 }
 63:         }

 65:         public void testAssertNullNotEqualsNull() {
 66:                 try {
 67:                         assertEquals(null, new Object());
 68:                 } catch (AssertionFailedError e) {
 69:                         e.getMessage(); // why no assertion?
 70:                         return;
 71:                 }
 72:                 fail();
 73:         }

 75:         public void testAssertNull() {
 76:                 assertNull(null);
 77:                 try {
 78:                         assertNull(new Object());
 79:                 } catch (AssertionFailedError e) {
 80:                         return;
 81:                 }
 82:                 fail();
 83:         }

 85:         public void testAssertNotNull() {
 86:                 assertNotNull(new Object());
 87:                 try {
 88:                         assertNotNull(null);
 89:                 } catch (AssertionFailedError e) {
 90:                         return;
 91:                 }
 92:                 fail();
 93:         }

 95:         public void testAssertTrue() {
 96:                 assertTrue(true);
 97:                 try {
 98:                         assertTrue(false);
 99:                 } catch (AssertionFailedError e) {
100:                         return;
101:                 }
102:                 fail();
103:         }

105:         public void testAssertFalse() {
106:                 assertFalse(false);
107:                 try {
108:                         assertFalse(true);
109:                 } catch (AssertionFailedError e) {
110:                         return;
111:                 }
112:                 fail();
113:         }

115:         public void testAssertSame() {
116:                 Object o= new Object();
117:                 assertSame(o, o);
118:                 try {
119:                         assertSame(new Integer(1), new Integer(1));
120:                 } catch (AssertionFailedError e) {
121:                         return;
122:                 }
123:                 fail();
124:         }

126:         public void testAssertNotSame() {
127:                 assertNotSame(new Integer(1), null);
128:                 assertNotSame(null, new Integer(1));
129:                 assertNotSame(new Integer(1), new Integer(1));
130:                 try {
131:                         Integer obj= new Integer(1);
132:                         assertNotSame(obj, obj);
133:                 } catch (AssertionFailedError e) {
134:                         return;
135:                 }
136:                 fail();
137:         }

139:         public void testAssertNotSameFailsNull() {
140:                 try {
141:                         assertNotSame(null, null);
142:                 } catch (AssertionFailedError e) {
143:                         return;
144:                 }
145:                 fail();
146:         }
147: }