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

Best Mockito code snippet using org.mockito.internal.creation.bytebuddy.AbstractByteBuddyMockMakerTest.

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1/**
2 * Copyright (c) 2017 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.creation.bytebuddy;
6
7
8import java.io.Serializable;
9import java.util.List;
10import net.bytebuddy.ByteBuddy;
11import org.junit.Assert;
12import org.junit.Test;
13import org.mockito.Mockito;
14import org.mockito.internal.handler.MockHandlerImpl;
15import org.mockito.invocation.Invocation;
16import org.mockito.invocation.InvocationContainer;
17import org.mockito.invocation.MockHandler;
18import org.mockito.mock.MockCreationSettings;
19import org.mockito.mock.SerializableMode;
20import org.mockito.plugins.MockMaker;
21import org.mockito.stubbing.Answer;
22import org.mockitoutil.ClassLoaders;
23import org.mockitoutil.SimpleSerializationUtil;
24import org.objenesis.ObjenesisStd;
25
26
27public abstract class AbstractByteBuddyMockMakerTest<MM extends MockMaker> {
28    protected final MM mockMaker;
29
30    public AbstractByteBuddyMockMakerTest(MM mockMaker) {
31        this.mockMaker = mockMaker;
32    }
33
34    @Test
35    public void should_create_mock_from_interface() throws Exception {
36        AbstractByteBuddyMockMakerTest.SomeInterface proxy = mockMaker.createMock(AbstractByteBuddyMockMakerTest.settingsFor(AbstractByteBuddyMockMakerTest.SomeInterface.class), AbstractByteBuddyMockMakerTest.dummyHandler());
37        Class<?> superClass = proxy.getClass().getSuperclass();
38        AbstractByteBuddyMockMakerTest.assertThat(superClass).isEqualTo(Object.class);
39    }
40
41    @Test
42    public void should_create_mock_from_class() throws Exception {
43        AbstractByteBuddyMockMakerTest<MM>.ClassWithoutConstructor proxy = mockMaker.createMock(AbstractByteBuddyMockMakerTest.settingsFor(AbstractByteBuddyMockMakerTest.ClassWithoutConstructor.class), AbstractByteBuddyMockMakerTest.dummyHandler());
44        Class<?> superClass = mockTypeOf(proxy.getClass());
45        AbstractByteBuddyMockMakerTest.assertThat(superClass).isEqualTo(AbstractByteBuddyMockMakerTest.ClassWithoutConstructor.class);
46    }
47
48    @Test
49    public void should_create_mock_from_class_even_when_constructor_is_dodgy() throws Exception {
50        try {
51            new ClassWithDodgyConstructor();
52            Assert.fail();
53        } catch (Exception expected) {
54        }
55        AbstractByteBuddyMockMakerTest<MM>.ClassWithDodgyConstructor mock = mockMaker.createMock(AbstractByteBuddyMockMakerTest.settingsFor(AbstractByteBuddyMockMakerTest.ClassWithDodgyConstructor.class), AbstractByteBuddyMockMakerTest.dummyHandler());
56        AbstractByteBuddyMockMakerTest.assertThat(mock).isNotNull();
57    }
58
59    @Test
60    public void should_mocks_have_different_interceptors() throws Exception {
61        AbstractByteBuddyMockMakerTest<MM>.SomeClass mockOne = mockMaker.createMock(AbstractByteBuddyMockMakerTest.settingsFor(AbstractByteBuddyMockMakerTest.SomeClass.class), AbstractByteBuddyMockMakerTest.dummyHandler());
62        AbstractByteBuddyMockMakerTest<MM>.SomeClass mockTwo = mockMaker.createMock(AbstractByteBuddyMockMakerTest.settingsFor(AbstractByteBuddyMockMakerTest.SomeClass.class), AbstractByteBuddyMockMakerTest.dummyHandler());
63        MockHandler handlerOne = mockMaker.getHandler(mockOne);
64        MockHandler handlerTwo = mockMaker.getHandler(mockTwo);
65        AbstractByteBuddyMockMakerTest.assertThat(handlerOne).isNotSameAs(handlerTwo);
66    }
67
68    @Test
69    public void should_use_ancillary_Types() {
70        AbstractByteBuddyMockMakerTest<MM>.SomeClass mock = mockMaker.createMock(AbstractByteBuddyMockMakerTest.settingsFor(AbstractByteBuddyMockMakerTest.SomeClass.class, AbstractByteBuddyMockMakerTest.SomeInterface.class), AbstractByteBuddyMockMakerTest.dummyHandler());
71        AbstractByteBuddyMockMakerTest.assertThat(mock).isInstanceOf(AbstractByteBuddyMockMakerTest.SomeInterface.class);
72    }
73
74    @Test
75    public void should_create_class_by_constructor() {
76        AbstractByteBuddyMockMakerTest.OtherClass mock = mockMaker.createMock(AbstractByteBuddyMockMakerTest.settingsWithConstructorFor(AbstractByteBuddyMockMakerTest.OtherClass.class), AbstractByteBuddyMockMakerTest.dummyHandler());
77        AbstractByteBuddyMockMakerTest.assertThat(mock).isNotNull();
78    }
79
80    @Test
81    public void should_allow_serialization() throws Exception {
82        AbstractByteBuddyMockMakerTest.SerializableClass proxy = mockMaker.createMock(AbstractByteBuddyMockMakerTest.serializableSettingsFor(AbstractByteBuddyMockMakerTest.SerializableClass.class, SerializableMode.BASIC), AbstractByteBuddyMockMakerTest.dummyHandler());
83        AbstractByteBuddyMockMakerTest.SerializableClass serialized = SimpleSerializationUtil.serializeAndBack(proxy);
84        AbstractByteBuddyMockMakerTest.assertThat(serialized).isNotNull();
85        MockHandler handlerOne = mockMaker.getHandler(proxy);
86        MockHandler handlerTwo = mockMaker.getHandler(serialized);
87        AbstractByteBuddyMockMakerTest.assertThat(handlerOne).isNotSameAs(handlerTwo);
88    }
89
90    @Test
91    public void should_create_mock_from_class_with_super_call_to_final_method() throws Exception {
92        MockCreationSettings<AbstractByteBuddyMockMakerTest.CallingSuperMethodClass> settings = AbstractByteBuddyMockMakerTest.settingsWithSuperCall(AbstractByteBuddyMockMakerTest.CallingSuperMethodClass.class);
93        AbstractByteBuddyMockMakerTest.SampleClass proxy = mockMaker.createMock(settings, new MockHandlerImpl<AbstractByteBuddyMockMakerTest.CallingSuperMethodClass>(settings));
94        AbstractByteBuddyMockMakerTest.assertThat(proxy.foo()).isEqualTo("foo");
95    }
96
97    @Test
98    public void should_reset_mock_and_set_new_handler() throws Throwable {
99        MockCreationSettings<AbstractByteBuddyMockMakerTest.SampleClass> settings = AbstractByteBuddyMockMakerTest.settingsWithSuperCall(AbstractByteBuddyMockMakerTest.SampleClass.class);
100        AbstractByteBuddyMockMakerTest.SampleClass proxy = mockMaker.createMock(settings, new MockHandlerImpl<AbstractByteBuddyMockMakerTest.SampleClass>(settings));
101        MockHandler handler = new MockHandlerImpl<AbstractByteBuddyMockMakerTest.SampleClass>(settings);
102        mockMaker.resetMock(proxy, handler, settings);
103        AbstractByteBuddyMockMakerTest.assertThat(mockMaker.getHandler(proxy)).isSameAs(handler);
104    }
105
106    class SomeClass {}
107
108    interface SomeInterface {}
109
110    static class OtherClass {}
111
112    static class SerializableClass implements Serializable {}
113
114    private class ClassWithoutConstructor {}
115
116    private class ClassWithDodgyConstructor {
117        public ClassWithDodgyConstructor() {
118            throw new RuntimeException();
119        }
120    }
121
122    @Test
123    public void instantiate_fine_when_objenesis_on_the_classpath() throws Exception {
124        // given
125        ClassLoader classpath_with_objenesis = ClassLoaders.excludingClassLoader().withCodeSourceUrlOf(Mockito.class, ByteBuddy.class, ObjenesisStd.class).withCodeSourceUrlOf(ClassLoaders.coverageTool()).build();
126        Class<?> mock_maker_class_loaded_fine_until = Class.forName("org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker", true, classpath_with_objenesis);
127        // when
128        mock_maker_class_loaded_fine_until.newInstance();
129        // then everything went fine
130    }
131
132    private static class DummyMockHandler implements MockHandler<Object> {
133        public Object handle(Invocation invocation) throws Throwable {
134            return null;
135        }
136
137        public MockCreationSettings<Object> getMockSettings() {
138            return null;
139        }
140
141        public InvocationContainer getInvocationContainer() {
142            return null;
143        }
144
145        public void setAnswersForStubbing(List<Answer<?>> list) {
146        }
147    }
148
149    private static class SampleClass {
150        public String foo() {
151            return "foo";
152        }
153    }
154
155    private static class CallingSuperMethodClass extends AbstractByteBuddyMockMakerTest.SampleClass {
156        @Override
157        public String foo() {
158            return super.foo();
159        }
160    }
161}
162
163
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
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)