How to use Platform class of org.mockito.internal.util package

Best Mockito code snippet using org.mockito.internal.util.Platform

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.android.internal.creation;
6
7import org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker;
8import org.mockito.internal.util.ConsoleMockitoLogger;
9import org.mockito.internal.util.Platform;
10import org.mockito.invocation.MockHandler;
11import org.mockito.mock.MockCreationSettings;
12import org.mockito.plugins.MockMaker;
13
14import static org.mockito.internal.util.StringUtil.join;
15
16public class AndroidByteBuddyMockMaker implements MockMaker {
17
18    private final MockMaker delegate;
19
20    public AndroidByteBuddyMockMaker() {
21        if (Platform.isAndroid() || Platform.isAndroidMockMakerRequired()) {
22            delegate = new SubclassByteBuddyMockMaker(new AndroidLoadingStrategy());
23        } else {
24            new ConsoleMockitoLogger().log(join(
25                    "IMPORTANT NOTE FROM MOCKITO:",
26                    "",
27                    "You included the 'mockito-android' dependency in a non-Android environment.",
28                    "The Android mock maker was disabled. You should only include the latter in your 'androidTestCompile' configuration",
29                    "If disabling was a mistake, you can set the 'org.mockito.mock.android' property to 'true' to override this detection.",
30                    "",
31                    "Visit https://javadoc.io/page/org.mockito/mockito-core/latest/org/mockito/Mockito.html#0.1 for more information"
32            ));
33            delegate = new SubclassByteBuddyMockMaker();
34        }
35    }
36
37    @Override
38    public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
39        return delegate.createMock(settings, handler);
40    }
41
42    @Override
43    public MockHandler getHandler(Object mock) {
44        return delegate.getHandler(mock);
45    }
46
47    @Override
48    public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
49        delegate.resetMock(mock, newHandler, settings);
50    }
51
52    @Override
53    public TypeMockability isTypeMockable(Class<?> type) {
54        return delegate.isTypeMockable(type);
55    }
56}
57
Full Screen
copy
1/*******************************************************************************
2 *  Copyright FUJITSU LIMITED 2017
3 *******************************************************************************/
4
5package org.oscm.ui.dialog.common.ldapsettings;
6
7import static org.junit.Assert.assertEquals;
8import static org.junit.Assert.assertFalse;
9import static org.junit.Assert.assertTrue;
10import static org.mockito.Matchers.eq;
11import static org.mockito.Matchers.same;
12import static org.mockito.Mockito.doNothing;
13import static org.mockito.Mockito.doReturn;
14import static org.mockito.Mockito.mock;
15import static org.mockito.Mockito.spy;
16import static org.mockito.Mockito.times;
17import static org.mockito.Mockito.verify;
18import static org.mockito.Mockito.verifyNoMoreInteractions;
19
20import java.io.ByteArrayInputStream;
21import java.io.IOException;
22import java.util.HashSet;
23import java.util.Properties;
24import java.util.Set;
25
26import org.junit.Before;
27import org.junit.Test;
28import org.mockito.ArgumentCaptor;
29import org.mockito.Captor;
30import org.mockito.MockitoAnnotations;
31
32import org.oscm.ui.beans.BaseBean;
33import org.oscm.ui.dialog.common.ldapsettings.LdapSetting;
34import org.oscm.ui.dialog.common.ldapsettings.LdapSettingConverter;
35import org.oscm.ui.dialog.common.ldapsettings.PlatformSettingCtrl;
36import org.oscm.ui.dialog.common.ldapsettings.PlatformSettingModel;
37import org.oscm.internal.usermanagement.POLdapSetting;
38import org.oscm.internal.usermanagement.UserManagementService;
39
40public class PlatformSettingCtrlTest {
41
42    private PlatformSettingCtrl ctrl;
43    private LdapSettingConverter converter;
44    private UserManagementService ums;
45    private PlatformSettingModel model;
46    private Set<POLdapSetting> serverSettings = new HashSet<POLdapSetting>();
47
48    @Captor
49    ArgumentCaptor<byte[]> writtenContent;
50
51    @Before
52    public void setup() throws IOException {
53        MockitoAnnotations.initMocks(this);
54        ctrl = spy(new PlatformSettingCtrl());
55        converter = spy(new LdapSettingConverter());
56        ums = mock(UserManagementService.class);
57        model = new PlatformSettingModel();
58        ctrl.setModel(model);
59
60        doReturn(converter).when(ctrl).getSettingConverter();
61        doReturn(ums).when(ctrl).getUserManagementService();
62        doReturn(serverSettings).when(ums).getPlatformSettings();
63        doNothing().when(ctrl).writeSettings(writtenContent.capture());
64    }
65
66    @Test
67    public void initModelData_Delegation() {
68        ctrl.initModelData();
69        verify(ums, times(1)).getPlatformSettings();
70        verifyNoMoreInteractions(ums);
71        verify(converter, times(1)).addToModel(
72                same(model.getPlatformSettings()), same(serverSettings));
73    }
74
75    @Test
76    public void exportSettings_Delegation() throws IOException {
77        String result = ctrl.exportSettings();
78        verify(converter, times(1)).toProperties(
79                same(model.getPlatformSettings()), eq(false));
80        assertEquals(BaseBean.OUTCOME_SUCCESS, result);
81    }
82
83    @Test
84    public void exportSettings_ContentValidation() throws IOException {
85        model.getPlatformSettings().add(new LdapSetting("key", "value", false));
86        ctrl.exportSettings();
87        ByteArrayInputStream bais = new ByteArrayInputStream(
88                writtenContent.getValue());
89        Properties props = new Properties();
90        props.load(bais);
91        assertEquals(1, props.keySet().size());
92        assertEquals("value", props.getProperty("key"));
93    }
94
95    @Test
96    public void isPlatformSettingsDefined_ContentValidation_Empty() {
97        assertFalse(ctrl.isPlatformSettingsDefined());
98    }
99
100    @Test
101    public void isPlatformSettingsDefined_ContentValidation_SettingsDefined() {
102        model.getPlatformSettings().add(new LdapSetting("key", "value", false));
103        assertTrue(ctrl.isPlatformSettingsDefined());
104    }
105
106}
107
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 Platform

    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)