Best Mockito code snippet using org.mockito.internal.exceptions.Reporter.delegatedMethodDoesNotExistOnDelegate
Source:ForwardsInvocations.java
2 * Copyright (c) 2007 Mockito contributors3 * This program is made available under the terms of the MIT License.4 */5package org.mockito.internal.stubbing.defaultanswers;6import static org.mockito.internal.exceptions.Reporter.delegatedMethodDoesNotExistOnDelegate;7import static org.mockito.internal.exceptions.Reporter.delegatedMethodHasWrongReturnType;8import java.io.Serializable;9import java.lang.reflect.InvocationTargetException;10import java.lang.reflect.Method;11import org.mockito.internal.configuration.plugins.Plugins;12import org.mockito.invocation.Invocation;13import org.mockito.invocation.InvocationOnMock;14import org.mockito.plugins.MemberAccessor;15import org.mockito.stubbing.Answer;16/**17 * Internal answer to forward invocations on a real instance.18 *19 * @since 1.9.520 */21public class ForwardsInvocations implements Answer<Object>, Serializable {22 private static final long serialVersionUID = -8343690268123254910L;23 private Object delegatedObject = null;24 public ForwardsInvocations(Object delegatedObject) {25 this.delegatedObject = delegatedObject;26 }27 public Object answer(InvocationOnMock invocation) throws Throwable {28 Method mockMethod = invocation.getMethod();29 try {30 Method delegateMethod = getDelegateMethod(mockMethod);31 if (!compatibleReturnTypes(32 mockMethod.getReturnType(), delegateMethod.getReturnType())) {33 throw delegatedMethodHasWrongReturnType(34 mockMethod, delegateMethod, invocation.getMock(), delegatedObject);35 }36 MemberAccessor accessor = Plugins.getMemberAccessor();37 Object[] rawArguments = ((Invocation) invocation).getRawArguments();38 return accessor.invoke(delegateMethod, delegatedObject, rawArguments);39 } catch (NoSuchMethodException e) {40 throw delegatedMethodDoesNotExistOnDelegate(41 mockMethod, invocation.getMock(), delegatedObject);42 } catch (InvocationTargetException e) {43 // propagate the original exception from the delegate44 throw e.getCause();45 }46 }47 private Method getDelegateMethod(Method mockMethod) throws NoSuchMethodException {48 if (mockMethod.getDeclaringClass().isAssignableFrom(delegatedObject.getClass())) {49 // Compatible class. Return original method.50 return mockMethod;51 } else {52 // Return method of delegate object with the same signature as mockMethod.53 return delegatedObject54 .getClass()...
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!!