How to use DefaultMockitoFramework class of org.mockito.internal.framework package

Best Mockito code snippet using org.mockito.internal.framework.DefaultMockitoFramework

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.test.framework;
6
7import org.junit.After;
8import org.junit.Test;
9import org.mockito.ArgumentMatchers;
10import org.mockito.MockSettings;
11import org.mockito.internal.framework.DefaultMockitoFramework;
12import org.mockito.test.StateMaster;
13import org.mockito.exceptions.misusing.RedundantListenerException;
14import org.mockito.listeners.MockCreationListener;
15import org.mockito.listeners.MockitoListener;
16import org.mockito.mock.MockCreationSettings;
17import org.mockito.test.mockitoutil.TestBase;
18
19import java.util.List;
20import java.util.Set;
21
22import static org.mockito.Mockito.*;
23import static org.mockito.test.mockitoutil.ThrowableAssert.assertThat;
24
25public class DefaultMockitoFrameworkTest extends TestBase {
26
27    private DefaultMockitoFramework framework = new DefaultMockitoFramework();
28
29    @After public void clearListeners() {
30        new StateMaster().clearMockitoListeners();
31    }
32
33    @Test(expected = IllegalArgumentException.class)
34    public void prevents_adding_null_listener() {
35        framework.addListener(null);
36    }
37
38    @Test(expected = IllegalArgumentException.class)
39    public void prevents_removing_null_listener() {
40        framework.removeListener(null);
41    }
42
43    @Test
44    public void ok_to_remove_unknown_listener() {
45        //it is safe to remove listener that was not added before
46        framework.removeListener(new MockitoListener() {});
47    }
48
49    @Test
50    public void ok_to_remove_listener_multiple_times() {
51        MockitoListener listener = new MockitoListener() {};
52
53        //when
54        framework.addListener(listener);
55
56        //then it is ok to:
57        framework.removeListener(listener);
58        framework.removeListener(listener);
59    }
60
61    @Test
62    public void adds_creation_listener() {
63        //given creation listener is added
64        MockCreationListener listener = mock(MockCreationListener.class);
65        framework.addListener(listener);
66
67        //when
68        MockSettings settings = withSettings().name("my list");
69        List mock = mock(List.class, settings);
70        Set mock2 = mock(Set.class);
71
72        //then
73        verify(listener).onMockCreated(eq(mock), any(MockCreationSettings.class));
74        verify(listener).onMockCreated(eq(mock2), any(MockCreationSettings.class));
75        verifyNoMoreInteractions(listener);
76    }
77
78    @Test
79    public void removes_creation_listener() {
80        //given creation listener is added
81        MockCreationListener listener = mock(MockCreationListener.class);
82        framework.addListener(listener);
83
84        //and hooked up correctly
85        mock(List.class);
86        verify(listener).onMockCreated(ArgumentMatchers.any(), any(MockCreationSettings.class));
87
88        //when
89        framework.removeListener(listener);
90        mock(Set.class);
91
92        //then
93        verifyNoMoreInteractions(listener);
94    }
95
96    @Test public void prevents_duplicate_listeners_of_the_same_type() {
97        //given creation listener is added
98        framework.addListener(new MyListener());
99
100        assertThat(new Runnable() {
101            @Override
102            public void run() {
103                framework.addListener(new MyListener());
104            }
105        })  .throwsException(RedundantListenerException.class)
106            .throwsMessage("\n" +
107                    "Problems adding Mockito listener.\n" +
108                    "Listener of type 'MyListener' has already been added and not removed.\n" +
109                    "It indicates that previous listener was not removed according to the API.\n" +
110                    "When you add a listener, don't forget to remove the listener afterwards:\n" +
111                    "  Mockito.framework().removeListener(myListener);\n" +
112                    "For more information, see the javadoc for RedundantListenerException class.");
113    }
114
115    private static class MyListener implements MockitoListener {}
116}
117
Full Screen
copy
1/*
2 * Copyright (c) 2016 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.framework;
6
7import org.mockito.MockitoFramework;
8import org.mockito.internal.configuration.plugins.Plugins;
9import org.mockito.internal.invocation.DefaultInvocationFactory;
10import org.mockito.internal.util.Checks;
11import org.mockito.invocation.InvocationFactory;
12import org.mockito.listeners.MockitoListener;
13import org.mockito.plugins.MockitoPlugins;
14
15import static org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress;
16
17public class DefaultMockitoFramework implements MockitoFramework {
18
19    public MockitoFramework addListener(MockitoListener listener) {
20        Checks.checkNotNull(listener, "listener");
21        mockingProgress().addListener(listener);
22        return this;
23    }
24
25    public MockitoFramework removeListener(MockitoListener listener) {
26        Checks.checkNotNull(listener, "listener");
27        mockingProgress().removeListener(listener);
28        return this;
29    }
30
31    @Override
32    public MockitoPlugins getPlugins() {
33        return Plugins.getPlugins();
34    }
35
36    @Override
37    public InvocationFactory getInvocationFactory() {
38        return new DefaultInvocationFactory();
39    }
40}
41
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 DefaultMockitoFramework

    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)