1 package junitx.framework; 2 3 import junit.framework.AssertionFailedError; 4 import junit.framework.TestCase; 5 6 10 public class ThrowableAssertTest 11 extends TestCase { 12 13 public ThrowableAssertTest(String name) { 14 super(name); 15 } 16 17 public void testSuccessEquals() { 18 Throwable expected = new TestThrowable("This is a message"); 19 Throwable actual = new TestThrowable("This is a message"); 20 21 ThrowableAssert.assertEquals(expected, actual); 22 } 23 24 public void testSuccessEqualsLocal() { 25 Throwable expected = new TestThrowable("This is a message", "This is another message"); 26 Throwable actual = new TestThrowable("This is a message", "This is another message"); 27 28 ThrowableAssert.assertEquals(expected, actual); 29 } 30 31 public void testFailClass() 32 throws Exception { 33 try { 34 Throwable expected = new TestThrowable("This is a message"); 35 Throwable actual = new NullPointerException ("This is a message"); 36 37 ThrowableAssert.assertEquals(expected, actual); 38 } catch (AssertionFailedError e) { 39 Assert.assertEquals("expected instance of class: <junitx.framework.ThrowableAssertTest$TestThrowable> but was of class: <java.lang.NullPointerException>", e.getMessage()); 40 return; 41 } 42 fail("Should have thrown exception"); 43 } 44 45 public void testFailMessage() 46 throws Exception { 47 try { 48 Throwable expected = new TestThrowable("This is a message"); 49 Throwable actual = new TestThrowable("This is a wrong message"); 50 51 ThrowableAssert.assertEquals(expected, actual); 52 } catch (AssertionFailedError e) { 53 return; 54 } 55 fail("Should have thrown exception"); 56 } 57 58 public void testFailLocalized() 59 throws Exception { 60 try { 61 Throwable expected = new TestThrowable("This is a message", "This is another message"); 62 Throwable actual = new TestThrowable("This is a message", "This is another wrong message"); 63 64 ThrowableAssert.assertEquals(expected, actual); 65 } catch (AssertionFailedError e) { 66 return; 67 } 68 fail("Should have thrown exception"); 69 } 70 71 public void testSuccessSimilar() { 72 Throwable expected = new TestThrowable("is a"); 73 Throwable actual = new TestThrowable("This is a message"); 74 75 ThrowableAssert.assertSimilar(expected, actual); 76 } 77 78 public void testSuccessSimilarLocal() { 79 Throwable expected = new TestThrowable("is another"); 80 Throwable actual = new TestThrowable("This is a message", "This is another message"); 81 82 ThrowableAssert.assertSimilar(expected, actual); 83 } 84 85 public void testFailSimilar() 86 throws Exception { 87 try { 88 Throwable expected = new TestThrowable("is another"); 89 Throwable actual = new TestThrowable("This is a message"); 90 91 ThrowableAssert.assertSimilar(expected, actual); 92 } catch (AssertionFailedError e) { 93 return; 94 } 95 fail("Should have thrown exception"); 96 } 97 98 public void testFailSimilarLocal() 99 throws Exception { 100 try { 101 Throwable expected = new TestThrowable("has"); 102 Throwable actual = new TestThrowable("This is a message", "This is another message"); 103 104 ThrowableAssert.assertSimilar(expected, actual); 105 } catch (AssertionFailedError e) { 106 return; 107 } 108 fail("Should have thrown exception"); 109 } 110 111 public static class TestThrowable 112 extends Throwable { 113 114 private String local; 115 116 public TestThrowable(String message) { 117 super(message); 118 this.local = message; 119 } 120 121 public TestThrowable(String message, String local) { 122 super(message); 123 this.local = local; 124 } 125 126 public String getLocalizedMessage() { 127 return local; 128 } 129 130 } 131 132 } | Popular Tags |