Best Easymock code snippet using org.easymock.internal.RecordState.requireMethodCall
Source:RecordState.java
...90 "calling verify is not allowed in record state"));91 }9293 public void andReturn(Object value) {94 requireMethodCall("return value");95 value = convertNumberClassIfNeccessary(value);96 requireAssignable(value);97 if (lastResult != null) {98 times(MocksControl.ONCE);99 }100 lastResult = Result.createReturnResult(value);101 }102103 public void andThrow(Throwable throwable) {104 requireMethodCall("Throwable");105 requireValidThrowable(throwable);106 if (lastResult != null) {107 times(MocksControl.ONCE);108 }109 lastResult = Result.createThrowResult(throwable);110 }111 112 public void andAnswer(IAnswer<?> answer) {113 requireMethodCall("answer");114 requireValidAnswer(answer);115 if (lastResult != null) {116 times(MocksControl.ONCE);117 }118 lastResult = Result.createAnswerResult(answer);119 }120121 public void andDelegateTo(Object delegateTo) {122 requireMethodCall("delegate");123 requireValidDelegation(delegateTo);124 if (lastResult != null) {125 times(MocksControl.ONCE);126 }127 lastResult = Result.createDelegatingResult(delegateTo);128 }129 130 public void andStubReturn(Object value) {131 requireMethodCall("stub return value");132 value = convertNumberClassIfNeccessary(value);133 requireAssignable(value);134 if (lastResult != null) {135 times(MocksControl.ONCE);136 }137 behavior.addStub(lastInvocation, Result.createReturnResult(value));138 lastInvocationUsed = true;139 }140141 @SuppressWarnings("deprecation")142 public void setDefaultReturnValue(Object value) {143 requireMethodCall("default return value");144 value = convertNumberClassIfNeccessary(value);145 requireAssignable(value);146 if (lastResult != null) {147 times(MocksControl.ONCE);148 }149 behavior.addStub(150 lastInvocation.withMatcher(org.easymock.MockControl.ALWAYS_MATCHER), Result151 .createReturnResult(value));152 lastInvocationUsed = true;153 }154155 public void asStub() {156 requireMethodCall("stub behavior");157 requireVoidMethod();158 behavior.addStub(lastInvocation, Result.createReturnResult(null));159 lastInvocationUsed = true;160 }161162 @SuppressWarnings("deprecation")163 public void setDefaultVoidCallable() {164 requireMethodCall("default void callable");165 requireVoidMethod();166 behavior.addStub(167 lastInvocation.withMatcher(org.easymock.MockControl.ALWAYS_MATCHER), Result168 .createReturnResult(null));169 lastInvocationUsed = true;170 }171172 public void andStubThrow(Throwable throwable) {173 requireMethodCall("stub Throwable");174 requireValidThrowable(throwable);175 if (lastResult != null) {176 times(MocksControl.ONCE);177 }178 behavior.addStub(lastInvocation, Result.createThrowResult(throwable));179 lastInvocationUsed = true;180 }181182 @SuppressWarnings("deprecation")183 public void setDefaultThrowable(Throwable throwable) {184 requireMethodCall("default Throwable");185 requireValidThrowable(throwable);186 if (lastResult != null) {187 times(MocksControl.ONCE);188 }189 behavior.addStub(190 lastInvocation.withMatcher(org.easymock.MockControl.ALWAYS_MATCHER), Result191 .createThrowResult(throwable));192 lastInvocationUsed = true;193 }194195 public void andStubAnswer(IAnswer<?> answer) {196 requireMethodCall("stub answer");197 requireValidAnswer(answer);198 if (lastResult != null) {199 times(MocksControl.ONCE);200 }201 behavior.addStub(lastInvocation, Result.createAnswerResult(answer));202 lastInvocationUsed = true;203 }204205 public void andStubDelegateTo(Object delegateTo) {206 requireMethodCall("stub delegate");207 requireValidDelegation(delegateTo);208 if (lastResult != null) {209 times(MocksControl.ONCE);210 }211 behavior.addStub(lastInvocation, Result212 .createDelegatingResult(delegateTo));213 lastInvocationUsed = true;214 }215 216 public void times(Range range) {217 requireMethodCall("times");218 requireLastResultOrVoidMethod();219220 behavior.addExpected(lastInvocation, lastResult != null ? lastResult221 : Result.createReturnResult(null), range);222 lastInvocationUsed = true;223 lastResult = null;224 }225226 private Object createNumberObject(Object value, Class<?> returnType) {227 if (!(value instanceof Number)) {228 return value;229 }230 Number number = (Number) value; 231 if (returnType.equals(Byte.TYPE)) {232 return number.byteValue();233 } else if (returnType.equals(Short.TYPE)) {234 return number.shortValue();235 } else if (returnType.equals(Character.TYPE)) {236 return (char) number.intValue();237 } else if (returnType.equals(Integer.TYPE)) {238 return number.intValue();239 } else if (returnType.equals(Long.TYPE)) {240 return number.longValue();241 } else if (returnType.equals(Float.TYPE)) {242 return number.floatValue();243 } else if (returnType.equals(Double.TYPE)) {244 return number.doubleValue();245 } else {246 return number;247 }248 }249250 private Object convertNumberClassIfNeccessary(Object o) {251 Class<?> returnType = lastInvocation.getMethod().getReturnType();252 return createNumberObject(o, returnType);253 }254255 @SuppressWarnings("deprecation")256 private void closeMethod() {257 if (lastInvocationUsed && lastResult == null) {258 return;259 }260 if (!isLastResultOrVoidMethod()) {261 throw new RuntimeExceptionWrapper(new IllegalStateException(262 "missing behavior definition for the preceding method call "263 + lastInvocation.toString()));264 }265 this.times(org.easymock.MockControl.ONE);266 }267268 public static Object emptyReturnValueFor(Class<?> type) {269 return type.isPrimitive() ? emptyReturnValues.get(type) : null;270 }271272 private void requireMethodCall(String failMessage) {273 if (lastInvocation == null) {274 throw new RuntimeExceptionWrapper(new IllegalStateException(275 "method call on the mock needed before setting "276 + failMessage));277 }278 }279280 private void requireAssignable(Object returnValue) {281 if (lastMethodIsVoidMethod()) {282 throw new RuntimeExceptionWrapper(new IllegalStateException(283 "void method cannot return a value"));284 }285 if (returnValue == null) {286 return;287 }288 Class<?> returnedType = lastInvocation.getMethod().getReturnType();289 if (returnedType.isPrimitive()) {290 returnedType = primitiveToWrapperType.get(returnedType);291292 }293 if (!returnedType.isAssignableFrom(returnValue.getClass())) {294 throw new RuntimeExceptionWrapper(new IllegalStateException(295 "incompatible return value type"));296 }297 }298299 private void requireValidThrowable(Throwable throwable) {300 if (throwable == null)301 throw new RuntimeExceptionWrapper(new NullPointerException(302 "null cannot be thrown"));303 if (isValidThrowable(throwable))304 return;305306 throw new RuntimeExceptionWrapper(new IllegalArgumentException(307 "last method called on mock cannot throw "308 + throwable.getClass().getName()));309 }310311 private void requireValidAnswer(IAnswer<?> answer) {312 if (answer == null)313 throw new RuntimeExceptionWrapper(new NullPointerException(314 "answer object must not be null"));315 }316317 private void requireValidDelegation(Object delegateTo) {318 if (delegateTo == null)319 throw new RuntimeExceptionWrapper(new NullPointerException(320 "delegated to object must not be null"));321 // Would be nice to validate delegateTo is implementing the mock322 // interface (not possible right now)323 }324325 private void requireLastResultOrVoidMethod() {326 if (isLastResultOrVoidMethod()) {327 return;328 }329 throw new RuntimeExceptionWrapper(new IllegalStateException(330 "last method called on mock is not a void method"));331 }332333 private void requireVoidMethod() {334 if (lastMethodIsVoidMethod()) {335 return;336 }337 throw new RuntimeExceptionWrapper(new IllegalStateException(338 "last method called on mock is not a void method"));339 }340341 private boolean isLastResultOrVoidMethod() {342 return lastResult != null || lastMethodIsVoidMethod();343 }344345 private boolean lastMethodIsVoidMethod() {346 Class<?> returnType = lastInvocation.getMethod().getReturnType();347 return returnType.equals(Void.TYPE);348 }349350 private boolean isValidThrowable(Throwable throwable) {351 if (throwable instanceof RuntimeException) {352 return true;353 }354 if (throwable instanceof Error) {355 return true;356 }357 Class<?>[] exceptions = lastInvocation.getMethod().getExceptionTypes();358 Class<?> throwableClass = throwable.getClass();359 for (Class<?> exception : exceptions) {360 if (exception.isAssignableFrom(throwableClass))361 return true;362 }363 return false;364 }365366 public void checkOrder(boolean value) {367 closeMethod();368 behavior.checkOrder(value);369 }370371 public void makeThreadSafe(boolean threadSafe) {372 behavior.makeThreadSafe(threadSafe);373 }374375 public void checkIsUsedInOneThread(boolean shouldBeUsedInOneThread) {376 behavior.shouldBeUsedInOneThread(shouldBeUsedInOneThread);377 }378 379 @SuppressWarnings("deprecation")380 public void setDefaultMatcher(org.easymock.ArgumentsMatcher matcher) {381 behavior.setDefaultMatcher(matcher);382 }383384 @SuppressWarnings("deprecation")385 public void setMatcher(Method method, org.easymock.ArgumentsMatcher matcher) {386 requireMethodCall("matcher");387 behavior.setMatcher(lastInvocation.getMethod(), matcher);388 }
...
requireMethodCall
Using AI Code Generation
1import org.easymock.EasyMock2import org.easymock.EasyMock.expect3import org.easymock.EasyMock.expectLastCall4import org.easymock.EasyMock.replay5import org.easymock.EasyMock.verify6import org.easymock.EasyMock.createMock7import org.easymock.EasyMock.createStrictMock8import org.easymock.EasyMock.createNiceMock9import org.easymock.EasyMock.createControl10import org.easymock.EasyMock.createStrictControl11import org.easymock.EasyMock.createNiceControl12import org.easymock.EasyMock.createRecorder13import org.easymock.EasyMock.createStrictRecorder14import org.easymock.EasyMock.createNiceRecorder15import org.easymock.EasyMock.expectLastCall
requireMethodCall
Using AI Code Generation
1import org.easymock.EasyMock2import org.easymock.EasyMock.*3import org.easymock.internal.RecordState4import org.easymock.internal.MocksControl5def mock = createMock(RecordState)6def control = createControl(MocksControl)7def mockState = createMock(RecordState)8def mockControl = createControl(MocksControl)9mockState.expectAndReturn(mock, "requireMethodCall", null, null)10mockState.expectAndReturn(mock, "andStubReturn", null, null)11control.expectAndReturn(mockControl, "createMock", null, mockState)12control.expectAndReturn(mockControl, "createControl", null, mockControl)13def mockControl = createControl(MocksControl)14def mockState = createMock(RecordState)15mockState.expectAndReturn(mock, "requireMethodCall", null, null)16mockState.expectAndReturn(mock, "andStubReturn", null, null)17mockControl.expectAndReturn(mockControl, "createMock", null, mockState)18mockControl.expectAndReturn(mockControl, "createControl", null, mockControl)19def mock = createMock(RecordState)20def control = createControl(MocksControl)21control.expectAndReturn(mockControl, "createMock", null, mock)22control.expectAndReturn(mockControl, "createControl", null, mockControl)23def mock = createMock(RecordState)24def control = createControl(MocksControl)25control.expectAndReturn(mockControl, "createMock", null, mock)26control.expectAndReturn(mockControl, "createControl", null, mockControl)27def mock = createMock(RecordState)28def control = createControl(MocksControl)29control.expectAndReturn(mockControl, "createMock", null, mock)30control.expectAndReturn(mockControl, "createControl", null, mockControl)31def mock = createMock(RecordState)32def control = createControl(MocksControl)33control.expectAndReturn(mockControl, "createMock", null, mock)34control.expectAndReturn(mockControl, "createControl", null, mockControl)35def mock = createMock(RecordState)36def control = createControl(MocksControl)37control.expectAndReturn(mockControl, "createMock", null, mock)38control.expectAndReturn(mockControl, "createControl", null, mockControl)39def mock = createMock(RecordState)40def control = createControl(M
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!