How to use InlineByteBuddyMockMaker class of org.mockito.internal.creation.bytebuddy package

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

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2package com.android.tools.idea.mockito;
3
4import javax.annotation.Nullable;
5import org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker;
6import org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker;
7import org.mockito.internal.util.concurrent.WeakConcurrentMap;
8import org.mockito.invocation.MockHandler;
9import org.mockito.mock.MockCreationSettings;
10import org.mockito.plugins.MockMaker;
11
12/**
13 * This class is a plugin for Mockito framework.
14 * <p>
15 * It provides {@linkplain MockMaker} implementation capable to use several {@linkplain MockMaker}s
16 * (e.g. inline and non-inline) in the same test. By default {@linkplain SwitchingMockMaker} prefers {@linkplain ByteBuddyMockMaker}, and
17 * uses {@linkplain InlineByteBuddyMockMaker} to create mocks only if the default {@linkplain ByteBuddyMockMaker} reports that it cannot
18 * mock the class.
19 * <p>
20 * {@linkplain InlineByteBuddyMockMaker} injects JVM agent to enable instrumentation, therefore {@linkplain SwitchingMockMaker} initializes
21 * it lazily only when it is really needed. {@linkplain InlineByteBuddyMockMaker} will never be initialized, if the test code does not
22 * exceed capabilities of the {@linkplain ByteBuddyMockMaker}.
23 * <p>
24 * Main goal of this plugin is to address performance issues introduced by the {@linkplain InlineByteBuddyMockMaker}.
25 * {@linkplain ByteBuddyMockMaker} is fast, and works fine in most cases. It is not fair to get performance penalty for the majority of the
26 * tests just because there are few tests in the same suite which need {@code inline} version. Developers should only pay for what they use.
27 *
28 * @see MockitoEx
29 */
30public class SwitchingMockMaker implements MockMaker {
31  private final ByteBuddyMockMaker byteBuddy = new ByteBuddyMockMaker();
32
33  private static class LazyInlineMockMaker {
34    static InlineByteBuddyMockMaker INSTANCE = new InlineByteBuddyMockMaker();
35  }
36
37  private final WeakConcurrentMap<Object, MockMaker> mockToMaker = new WeakConcurrentMap.WithInlinedExpunction<>();
38
39  @Override
40  public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
41    MockMaker makerToUse;
42    makerToUse = selectMakerForType(settings.getTypeToMock());
43    T mock = makerToUse.createMock(settings, handler);
44    mockToMaker.put(mock, makerToUse);
45    return mock;
46  }
47
48  private <T> MockMaker selectMakerForType(Class<T> typeToMock) {
49    MockMaker makerToUse;
50    if (MockitoEx.forceInlineMockMaker || !byteBuddy.isTypeMockable(typeToMock).mockable()) {
51      makerToUse = LazyInlineMockMaker.INSTANCE;
52    }
53    else {
54      makerToUse = byteBuddy;
55    }
56    return makerToUse;
57  }
58
59  @Nullable
60  private MockMaker getMakerForMock(Object mock) {
61    return mockToMaker.get(mock);
62  }
63
64  @Override
65  public MockHandler getHandler(Object mock) {
66    MockMaker maker = getMakerForMock(mock);
67    if (maker != null) {
68      return maker.getHandler(mock);
69    }
70    else {
71      return null;
72    }
73  }
74
75
76  @Override
77  public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
78    MockMaker maker = getMakerForMock(mock);
79    if (maker != null) {
80      maker.resetMock(mock, newHandler, settings);
81    }
82  }
83
84  @Override
85  public TypeMockability isTypeMockable(Class<?> type) {
86    TypeMockability defaultAnswer = byteBuddy.isTypeMockable(type);
87    if (!defaultAnswer.mockable()) {
88      // most likely, LazyInlineMockMaker.INSTANCE will answer "true". But we don't want to instantiate it unless really needed.
89      return LazyInlineMockMaker.INSTANCE.isTypeMockable(type);
90    }
91    else {
92      return defaultAnswer;
93    }
94  }
95}
96
Full Screen
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.configuration.plugins;
6
7import org.junit.Test;
8import org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker;
9import org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker;
10import org.mockito.plugins.InstantiatorProvider;
11import org.mockito.plugins.InstantiatorProvider2;
12import org.mockito.plugins.MockMaker;
13import org.mockitoutil.TestBase;
14
15import static org.junit.Assert.*;
16import static org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.INLINE_ALIAS;
17
18public class DefaultMockitoPluginsTest extends TestBase {
19
20    private DefaultMockitoPlugins plugins = new DefaultMockitoPlugins();
21
22    @Test
23    public void provides_plugins() throws Exception {
24        assertEquals("org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker",
25            plugins.getDefaultPluginClass(INLINE_ALIAS));
26        assertEquals(InlineByteBuddyMockMaker.class, plugins.getInlineMockMaker().getClass());
27        assertEquals(ByteBuddyMockMaker.class, plugins.getDefaultPlugin(MockMaker.class).getClass());
28        assertNotNull(plugins.getDefaultPlugin(InstantiatorProvider.class));
29        assertNotNull(plugins.getDefaultPlugin(InstantiatorProvider2.class));
30    }
31}
32
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 InlineByteBuddyMockMaker

    No relevant questions 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)