1 5 package org.easymock.tests; 6 7 import junit.framework.AssertionFailedError; 8 import junit.framework.TestCase; 9 10 import org.easymock.MockControl; 11 import org.easymock.internal.ReplayState; 12 13 public class UsageStrictMockTest extends TestCase { 14 private MockControl<IMethods> control; 15 16 private IMethods mock; 17 18 protected void setUp() { 19 control = MockControl.createStrictControl(IMethods.class); 20 mock = control.getMock(); 21 22 mock.simpleMethodWithArgument("1"); 23 mock.simpleMethodWithArgument("2"); 24 25 control.replay(); 26 } 27 28 public void testOrderedCallsSucces() { 29 mock.simpleMethodWithArgument("1"); 30 mock.simpleMethodWithArgument("2"); 31 32 control.verify(); 33 } 34 35 public void testUnorderedCallsFailure() { 36 boolean failed = false; 37 try { 38 mock.simpleMethodWithArgument("2"); 39 } catch (AssertionFailedError expected) { 40 failed = true; 41 } 42 if (!failed) { 43 fail("unordered calls accepted"); 44 } 45 } 46 47 public void testTooManyCallsFailure() { 48 mock.simpleMethodWithArgument("1"); 49 mock.simpleMethodWithArgument("2"); 50 51 boolean failed = false; 52 try { 53 mock.simpleMethodWithArgument("2"); 54 } catch (AssertionFailedError expected) { 55 failed = true; 56 } 57 if (!failed) { 58 fail("too many calls accepted"); 59 } 60 } 61 62 public void testTooFewCallsFailure() { 63 mock.simpleMethodWithArgument("1"); 64 boolean failed = false; 65 try { 66 control.verify(); 67 } catch (AssertionFailedError expected) { 68 failed = true; 69 assertTrue("stack trace must be filled in", Util.getStackTrace( 70 expected).indexOf(ReplayState.class.getName()) == -1); 71 } 72 if (!failed) { 73 fail("too few calls accepted"); 74 } 75 } 76 77 public void testDifferentMethods() { 78 79 control.reset(); 80 81 mock.booleanReturningMethod(0); 82 control.setReturnValue(true); 83 mock.simpleMethod(); 84 mock.booleanReturningMethod(1); 85 control.setReturnValue(false, 2, 3); 86 mock.simpleMethod(); 87 control.setVoidCallable(MockControl.ONE_OR_MORE); 88 89 control.replay(); 90 assertEquals(true, mock.booleanReturningMethod(0)); 91 mock.simpleMethod(); 92 93 boolean failed = false; 94 try { 95 control.verify(); 96 } catch (AssertionFailedError expected) { 97 failed = true; 98 assertEquals( 99 "\n Expectation failure on verify:" 100 + "\n simpleMethod(): expected: 1, actual: 1" 101 + "\n booleanReturningMethod(1): expected: between 2 and 3, actual: 0" 102 + "\n simpleMethod(): expected: at least 1, actual: 0", 103 expected.getMessage()); 104 } 105 if (!failed) { 106 fail("too few calls accepted"); 107 } 108 109 assertEquals(false, mock.booleanReturningMethod(1)); 110 111 failed = false; 112 try { 113 mock.simpleMethod(); 114 } catch (AssertionFailedError expected) { 115 failed = true; 116 assertEquals( 117 "\n Unexpected method call simpleMethod():" 118 + "\n simpleMethod(): expected: 0, actual: 1" 119 + "\n booleanReturningMethod(1): expected: between 2 and 3, actual: 1", 120 expected.getMessage()); 121 } 122 if (!failed) { 123 fail("wrong call accepted"); 124 } 125 } 126 127 public void testRange() { 128 129 control.reset(); 130 131 mock.booleanReturningMethod(0); 132 control.setReturnValue(true); 133 mock.simpleMethod(); 134 mock.booleanReturningMethod(1); 135 control.setReturnValue(false, 2, 3); 136 mock.simpleMethod(); 137 control.setVoidCallable(MockControl.ONE_OR_MORE); 138 mock.booleanReturningMethod(1); 139 control.setReturnValue(false); 140 141 control.replay(); 142 143 mock.booleanReturningMethod(0); 144 mock.simpleMethod(); 145 146 mock.booleanReturningMethod(1); 147 mock.booleanReturningMethod(1); 148 mock.booleanReturningMethod(1); 149 150 boolean failed = false; 151 152 try { 153 mock.booleanReturningMethod(1); 154 } catch (AssertionFailedError expected) { 155 failed = true; 156 assertEquals( 157 "\n Unexpected method call booleanReturningMethod(1):" 158 + "\n booleanReturningMethod(1): expected: between 2 and 3, actual: 4" 159 + "\n simpleMethod(): expected: at least 1, actual: 0", 160 expected.getMessage()); 161 } 162 if (!failed) { 163 fail("too many calls accepted"); 164 } 165 } 166 167 public void testDefaultBehavior() { 168 control.reset(); 169 170 mock.booleanReturningMethod(1); 171 control.setReturnValue(true); 172 control.setReturnValue(false); 173 control.setReturnValue(true); 174 control.setDefaultReturnValue(true); 175 176 control.replay(); 177 178 assertEquals(true, mock.booleanReturningMethod(2)); 179 assertEquals(true, mock.booleanReturningMethod(3)); 180 assertEquals(true, mock.booleanReturningMethod(1)); 181 assertEquals(false, mock.booleanReturningMethod(1)); 182 assertEquals(true, mock.booleanReturningMethod(3)); 183 184 boolean failed = false; 185 try { 186 control.verify(); 187 } catch (AssertionFailedError expected) { 188 failed = true; 189 assertEquals( 190 "\n Expectation failure on verify:" 191 + "\n booleanReturningMethod(1): expected: 3, actual: 2", 192 expected.getMessage()); 193 } 194 if (!failed) { 195 fail("too few calls accepted"); 196 } 197 } 198 199 public void testUnexpectedCallWithArray() { 200 control.reset(); 201 control.setDefaultMatcher(MockControl.ARRAY_MATCHER); 202 mock.arrayMethod(new String [] { "Test", "Test 2" }); 203 control.replay(); 204 boolean failed = false; 205 String [] strings = new String [] { "Test" }; 206 try { 207 mock.arrayMethod(strings); 208 } catch (AssertionFailedError expected) { 209 failed = true; 210 assertEquals( 211 "\n Unexpected method call arrayMethod([\"Test\"]):" 212 + "\n arrayMethod([\"Test\"]): expected: 0, actual: 1" 213 + "\n arrayMethod([\"Test\", \"Test 2\"]): expected: 1, actual: 0", 214 expected.getMessage()); 215 } 216 if (!failed) { 217 fail("exception expected"); 218 } 219 220 } 221 } 222 | Popular Tags |