How to use doWriteReplace method of org.mockito.internal.creation.bytebuddy.MockMethodInterceptor class

Best Mockito code snippet using org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doWriteReplace

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1package org.mockito.internal.creation.bytebuddy;
2
3import java.io.ObjectStreamException;
4import java.io.Serializable;
5import java.lang.reflect.Method;
6import java.util.concurrent.Callable;
7import org.mockito.internal.InternalMockHandler;
8import org.mockito.internal.creation.DelegatingMethod;
9import org.mockito.internal.invocation.MockitoMethod;
10import org.mockito.internal.invocation.SerializableMethod;
11import org.mockito.internal.progress.SequenceNumber;
12import org.mockito.invocation.MockHandler;
13import org.mockito.mock.MockCreationSettings;
14import net.bytebuddy.implementation.bind.annotation.AllArguments;
15import net.bytebuddy.implementation.bind.annotation.Argument;
16import net.bytebuddy.implementation.bind.annotation.BindingPriority;
17import net.bytebuddy.implementation.bind.annotation.DefaultCall;
18import net.bytebuddy.implementation.bind.annotation.Origin;
19import net.bytebuddy.implementation.bind.annotation.RuntimeType;
20import net.bytebuddy.implementation.bind.annotation.SuperCall;
21import net.bytebuddy.implementation.bind.annotation.This;
22
23public class MockMethodInterceptor implements Serializable {
24
25    private static final long serialVersionUID = 7152947254057253027L;
26
27    private final InternalMockHandler handler;
28    private final MockCreationSettings mockCreationSettings;
29
30    private final ByteBuddyCrossClassLoaderSerializationSupport serializationSupport;
31
32    public MockMethodInterceptor(InternalMockHandler handler, MockCreationSettings mockCreationSettings) {
33        this.handler = handler;
34        this.mockCreationSettings = mockCreationSettings;
35        serializationSupport = new ByteBuddyCrossClassLoaderSerializationSupport();
36    }
37
38    @RuntimeType
39    @BindingPriority(BindingPriority.DEFAULT * 3)
40    public Object interceptSuperCallable(@This Object mock,
41                                         @Origin(cache = true) Method invokedMethod,
42                                         @AllArguments Object[] arguments,
43                                         @SuperCall(serializableProxy = true) Callable<?> superCall) throws Throwable {
44        return doIntercept(
45                mock,
46                invokedMethod,
47                arguments,
48                new InterceptedInvocation.SuperMethod.FromCallable(superCall)
49        );
50    }
51
52    @RuntimeType
53    @BindingPriority(BindingPriority.DEFAULT * 2)
54    public Object interceptDefaultCallable(@This Object mock,
55                                           @Origin(cache = true) Method invokedMethod,
56                                           @AllArguments Object[] arguments,
57                                           @DefaultCall(serializableProxy = true) Callable<?> superCall) throws Throwable {
58        return doIntercept(
59                mock,
60                invokedMethod,
61                arguments,
62                new InterceptedInvocation.SuperMethod.FromCallable(superCall)
63        );
64    }
65
66    @RuntimeType
67    public Object interceptAbstract(@This Object mock,
68                                    @Origin(cache = true) Method invokedMethod,
69                                    @AllArguments Object[] arguments) throws Throwable {
70        return doIntercept(
71                mock,
72                invokedMethod,
73                arguments,
74                InterceptedInvocation.SuperMethod.IsIllegal.INSTANCE
75        );
76    }
77
78    private Object doIntercept(Object mock,
79                               Method invokedMethod,
80                               Object[] arguments,
81                               InterceptedInvocation.SuperMethod superMethod) throws Throwable {
82        return handler.handle(new InterceptedInvocation(
83                mock,
84                createMockitoMethod(invokedMethod),
85                arguments,
86                superMethod,
87                SequenceNumber.next()
88        ));
89    }
90
91    private MockitoMethod createMockitoMethod(Method method) {
92        if (mockCreationSettings.isSerializable()) {
93            return new SerializableMethod(method);
94        } else {
95            return new DelegatingMethod(method);
96        }
97    }
98
99    public MockHandler getMockHandler() {
100        return handler;
101    }
102
103    public ByteBuddyCrossClassLoaderSerializationSupport getSerializationSupport() {
104        return serializationSupport;
105    }
106
107    public static class ForHashCode {
108        public static int doIdentityHashCode(@This Object thiz) {
109            return System.identityHashCode(thiz);
110        }
111    }
112
113    public static class ForEquals {
114        public static boolean doIdentityEquals(@This Object thiz, @Argument(0) Object other) {
115            return thiz == other;
116        }
117    }
118
119    public static class ForWriteReplace {
120        public static Object doWriteReplace(@This MockAccess thiz) throws ObjectStreamException {
121            return thiz.getMockitoInterceptor().getSerializationSupport().writeReplace(thiz);
122        }
123    }
124
125    public static interface MockAccess {
126        MockMethodInterceptor getMockitoInterceptor();
127        void setMockitoInterceptor(MockMethodInterceptor mockMethodInterceptor);
128    }
129}
130
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
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