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