How to use MockitoMethod class of org.mockito.internal.invocation package

Best Mockito code snippet using org.mockito.internal.invocation.MockitoMethod

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5
6package org.powermock.api.mockito.repackaged;
7
8import org.mockito.cglib.proxy.MethodInterceptor;
9import org.mockito.cglib.proxy.MethodProxy;
10import org.mockito.internal.InternalMockHandler;
11import org.mockito.internal.creation.DelegatingMethod;
12import org.mockito.internal.creation.util.MockitoMethodProxy;
13import org.mockito.internal.invocation.InvocationImpl;
14import org.mockito.internal.invocation.MockitoMethod;
15import org.mockito.internal.invocation.SerializableMethod;
16import org.mockito.internal.invocation.realmethod.CleanTraceRealMethod;
17import org.mockito.internal.progress.SequenceNumber;
18import org.mockito.internal.util.ObjectMethodsGuru;
19import org.mockito.invocation.Invocation;
20import org.mockito.invocation.MockHandler;
21import org.mockito.mock.MockCreationSettings;
22
23import java.io.Serializable;
24import java.lang.reflect.Method;
25
26/**
27 * Should be one instance per mock instance, see CglibMockMaker.
28 */
29public class MethodInterceptorFilter implements MethodInterceptor, Serializable {
30
31    private static final long serialVersionUID = 6182795666612683784L;
32    private final InternalMockHandler handler;
33    final ObjectMethodsGuru objectMethodsGuru = new ObjectMethodsGuru();
34    private final MockCreationSettings mockSettings;
35    private final AcrossJVMSerializationFeature acrossJVMSerializationFeature = new AcrossJVMSerializationFeature();
36
37    public MethodInterceptorFilter(InternalMockHandler handler, MockCreationSettings mockSettings) {
38        this.handler = handler;
39        this.mockSettings = mockSettings;
40    }
41
42    @Override
43    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
44            throws Throwable {
45        if (objectMethodsGuru.isEqualsMethod(method)) {
46            return proxy == args[0];
47        } else if (objectMethodsGuru.isHashCodeMethod(method)) {
48            return hashCodeForMock(proxy);
49        } else if (acrossJVMSerializationFeature.isWriteReplace(method)) {
50            return acrossJVMSerializationFeature.writeReplace(proxy);
51        }
52
53        MockitoMethodProxy mockitoMethodProxy = createMockitoMethodProxy(methodProxy);
54        new CGLIBHacker().setMockitoNamingPolicy(methodProxy);
55
56        MockitoMethod mockitoMethod = createMockitoMethod(method);
57
58        CleanTraceRealMethod realMethod = new CleanTraceRealMethod(mockitoMethodProxy);
59        Invocation invocation = new InvocationImpl(proxy, mockitoMethod, args, SequenceNumber.next(), realMethod);
60        return handler.handle(invocation);
61    }
62
63    public MockHandler getHandler() {
64        return handler;
65    }
66
67    private int hashCodeForMock(Object mock) {
68        return System.identityHashCode(mock);
69    }
70
71    public MockitoMethodProxy createMockitoMethodProxy(MethodProxy methodProxy) {
72        if (mockSettings.isSerializable())
73            return new SerializableMockitoMethodProxy(methodProxy);
74        return new DelegatingMockitoMethodProxy(methodProxy);
75    }
76
77    public MockitoMethod createMockitoMethod(Method method) {
78        if (mockSettings.isSerializable()) {
79            return new SerializableMethod(method);
80        } else {
81            return new DelegatingMethod(method);
82        }
83    }
84}
85
Full Screen
copy
1/*
2 * Copyright (c) 2016 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.invocation;
6
7import org.mockito.internal.invocation.mockref.MockReference;
8import org.mockito.internal.exceptions.VerificationAwareInvocation;
9import org.mockito.internal.reporting.PrintSettings;
10import org.mockito.invocation.Invocation;
11import org.mockito.invocation.Location;
12import org.mockito.invocation.StubInfo;
13
14import java.lang.reflect.Method;
15import java.util.Arrays;
16
17import static org.mockito.internal.exceptions.Reporter.cannotCallAbstractRealMethod;
18
19public class InterceptedInvocation implements Invocation, VerificationAwareInvocation {
20
21    private static final long serialVersionUID = 475027563923510472L;
22
23    private final MockReference<Object> mockRef;
24    private final MockitoMethod mockitoMethod;
25    private final Object[] arguments, rawArguments;
26    private final RealMethod realMethod;
27
28    private final int sequenceNumber;
29
30    private final Location location;
31
32    private boolean verified;
33    private boolean isIgnoredForVerification;
34    private StubInfo stubInfo;
35
36    public InterceptedInvocation(MockReference<Object> mockRef,
37                                 MockitoMethod mockitoMethod,
38                                 Object[] arguments,
39                                 RealMethod realMethod,
40                                 Location location,
41                                 int sequenceNumber) {
42        this.mockRef = mockRef;
43        this.mockitoMethod = mockitoMethod;
44        this.arguments = ArgumentsProcessor.expandArgs(mockitoMethod, arguments);
45        this.rawArguments = arguments;
46        this.realMethod = realMethod;
47        this.location = location;
48        this.sequenceNumber = sequenceNumber;
49    }
50
51    @Override
52    public boolean isVerified() {
53        return verified || isIgnoredForVerification;
54    }
55
56    @Override
57    public int getSequenceNumber() {
58        return sequenceNumber;
59    }
60
61    @Override
62    public Location getLocation() {
63        return location;
64    }
65
66    @Override
67    public Object[] getRawArguments() {
68        return rawArguments;
69    }
70
71    @Override
72    public Class<?> getRawReturnType() {
73        return mockitoMethod.getReturnType();
74    }
75
76    @Override
77    public void markVerified() {
78        verified = true;
79    }
80
81    @Override
82    public StubInfo stubInfo() {
83        return stubInfo;
84    }
85
86    @Override
87    public void markStubbed(StubInfo stubInfo) {
88        this.stubInfo = stubInfo;
89    }
90
91    @Override
92    public boolean isIgnoredForVerification() {
93        return isIgnoredForVerification;
94    }
95
96    @Override
97    public void ignoreForVerification() {
98        isIgnoredForVerification = true;
99    }
100
101    @Override
102    public Object getMock() {
103        return mockRef.get();
104    }
105
106    @Override
107    public Method getMethod() {
108        return mockitoMethod.getJavaMethod();
109    }
110
111    @Override
112    public Object[] getArguments() {
113        return arguments;
114    }
115
116    @Override
117    @SuppressWarnings("unchecked")
118    public <T> T getArgument(int index) {
119        return (T) arguments[index];
120    }
121
122    @Override
123    public Object callRealMethod() throws Throwable {
124        if (!realMethod.isInvokable()) {
125            throw cannotCallAbstractRealMethod();
126        }
127        return realMethod.invoke();
128    }
129
130    @Override
131    public int hashCode() {
132        //TODO SF we need to provide hash code implementation so that there are no unexpected, slight perf issues
133        return 1;
134    }
135
136    @Override
137    public boolean equals(Object o) {
138        if (o == null || !o.getClass().equals(this.getClass())) {
139            return false;
140        }
141        InterceptedInvocation other = (InterceptedInvocation) o;
142        return this.mockRef.get().equals(other.mockRef.get())
143                && this.mockitoMethod.equals(other.mockitoMethod)
144                && this.equalArguments(other.arguments);
145    }
146
147    private boolean equalArguments(Object[] arguments) {
148        return Arrays.equals(arguments, this.arguments);
149    }
150
151    public String toString() {
152        return new PrintSettings().print(ArgumentsProcessor.argumentsToMatchers(getArguments()), this);
153    }
154
155    public final static RealMethod NO_OP = new RealMethod() {
156        public boolean isInvokable() {
157            return false;
158        }
159        public Object invoke() throws Throwable {
160            return null;
161        }
162    };
163
164}
165
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on MockitoMethod

    No relevant questions found for this class 😞

Most used methods in MockitoMethod

    No methods found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)