1 3 package test.jmock.expectation; 4 5 import java.util.Vector ; 6 import junit.framework.AssertionFailedError; 7 import junit.framework.TestCase; 8 import org.jmock.expectation.AssertMo; 9 10 11 public class AssertMoTest extends TestCase 12 { 13 14 public void testAssertExcludes() { 15 AssertMo.assertExcludes("Should not include substring", 16 "dog", 17 "The quick brown fox"); 18 } 19 20 public void testAssertExcludesFails() { 21 Throwable result = null; 22 try { 23 AssertMo.assertExcludes("Should fail on exclude", 24 "fox", 25 "The quick brown fox"); 26 } 27 catch (AssertionFailedError ex) { 28 result = ex; 29 } 30 31 assertTrue("Should get an exception", result != null); 32 33 } 34 35 public void testAssertIncludes() { 36 AssertMo.assertIncludes("Should include a substring", 37 "fox", 38 "The quick brown fox"); 39 } 40 41 public void testAssertIncludesFails() { 42 Throwable result = null; 43 try { 44 AssertMo.assertIncludes("Should fail if no include", 45 "dog", 46 "The quick brown fox"); 47 } 48 catch (AssertionFailedError ex) { 49 result = ex; 50 } 51 52 assertTrue("Should get an exception", result != null); 53 54 } 55 56 public void testAssertStartsWith() { 57 AssertMo.assertStartsWith("Should start with fox", 58 "fox", 59 "fox quick brown"); 60 } 61 62 public void testAssertStartsWithFails() { 63 Throwable result = null; 64 try { 65 AssertMo.assertStartsWith("Should fail if it doesn't start with fox", 66 "fox", 67 "The quick brown fox"); 68 } 69 catch (AssertionFailedError ex) { 70 result = ex; 71 } 72 73 assertTrue("Should get an exception", result != null); 74 75 } 76 77 public void testDifferentArrays() { 78 Object [] anExpectedArray = new Object []{"one", new Integer (2)}; 79 Object [] anActualArray = new Object []{"two", new Integer (2)}; 80 81 boolean threwException = false; 82 try { 83 AssertMo.assertEquals("Should be expected value", 84 anExpectedArray, 85 anActualArray); 86 } 87 catch (AssertionFailedError ignoredException) { 88 threwException = true; 89 } 90 assertTrue("should have thrown assertion failure", threwException); 91 } 92 93 public void testDifferentLengthArrays() { 94 Object [] anExpectedArray = new Object []{"one", new Integer (2)}; 95 Object [] anActualArray = new Object []{"one"}; 96 97 boolean threwException = false; 98 try { 99 AssertMo.assertEquals("Should be expected value", 100 anExpectedArray, 101 anActualArray); 102 } 103 catch (AssertionFailedError ignoredException) { 104 threwException = true; 105 } 106 assertTrue("should have thrown assertion failure", threwException); 107 } 108 109 public void testDifferentObjectArrays() { 110 Object [] anExpectedArray = new Object []{"one", new Integer (2)}; 111 Object [] anActualArray = new Object []{new Integer (2), new Vector ()}; 112 113 boolean threwException = false; 114 try { 115 AssertMo.assertEquals("Should be expected value", 116 anExpectedArray, 117 anActualArray); 118 } 119 catch (AssertionFailedError ignoredException) { 120 threwException = true; 121 } 122 assertTrue("should have thrown assertion failure", threwException); 123 } 124 125 public void testEqualArrays() { 126 Object [] anExpectedArray = new Object []{"one", new Integer (2)}; 127 Object [] anActualArray = new Object []{"one", new Integer (2)}; 128 129 AssertMo.assertEquals("Should be expected value", 130 anExpectedArray, 131 anActualArray); 132 } 133 134 public void testEqualEmptyArrays() { 135 Object [] anExpectedArray = new Object [0]; 136 Object [] anActualArray = new Object [0]; 137 138 AssertMo.assertEquals("Should be expected value", 139 anExpectedArray, 140 anActualArray); 141 } 142 143 public void testFailureCheckerWithFailure() { 144 AssertMo.assertFails("Test Description", 145 new Runnable () 146 { 147 public void run() { 148 fail("Should not be propagated"); 149 } 150 }); 151 } 152 153 public void testFailureCheckerWithoutFailure() { 154 final String TEST_MESSAGE = "Test Description"; 155 try { 156 AssertMo.assertFails(TEST_MESSAGE, new Runnable () 157 { 158 public void run() { 159 } 160 }); 161 } 162 catch (AssertionFailedError expected) { 163 assertEquals(TEST_MESSAGE, expected.getMessage()); 164 return; 165 } 166 fail("Should have thrown an exception"); 167 } 168 169 } 170 | Popular Tags |