How to use MockitoFramework class of org.mockito package

Best Mockito code snippet using org.mockito.MockitoFramework

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1package org.mockito.internal.framework;
2
3import com.google.android.gms.common.internal.ServiceSpecificExtraArgs;
4import org.mockito.MockitoFramework;
5import org.mockito.internal.configuration.plugins.Plugins;
6import org.mockito.internal.invocation.DefaultInvocationFactory;
7import org.mockito.internal.progress.ThreadSafeMockingProgress;
8import org.mockito.internal.util.Checks;
9import org.mockito.invocation.InvocationFactory;
10import org.mockito.listeners.MockitoListener;
11import org.mockito.plugins.InlineMockMaker;
12import org.mockito.plugins.MockMaker;
13import org.mockito.plugins.MockitoPlugins;
14
15public class DefaultMockitoFramework implements MockitoFramework {
16    public MockitoFramework addListener(MockitoListener mockitoListener) {
17        Checks.checkNotNull(mockitoListener, ServiceSpecificExtraArgs.CastExtraArgs.LISTENER);
18        ThreadSafeMockingProgress.mockingProgress().addListener(mockitoListener);
19        return this;
20    }
21
22    public MockitoFramework removeListener(MockitoListener mockitoListener) {
23        Checks.checkNotNull(mockitoListener, ServiceSpecificExtraArgs.CastExtraArgs.LISTENER);
24        ThreadSafeMockingProgress.mockingProgress().removeListener(mockitoListener);
25        return this;
26    }
27
28    public MockitoPlugins getPlugins() {
29        return Plugins.getPlugins();
30    }
31
32    public InvocationFactory getInvocationFactory() {
33        return new DefaultInvocationFactory();
34    }
35
36    private InlineMockMaker getInlineMockMaker() {
37        MockMaker mockMaker = Plugins.getMockMaker();
38        if (mockMaker instanceof InlineMockMaker) {
39            return (InlineMockMaker) mockMaker;
40        }
41        return null;
42    }
43
44    public void clearInlineMocks() {
45        InlineMockMaker inlineMockMaker = getInlineMockMaker();
46        if (inlineMockMaker != null) {
47            inlineMockMaker.clearAllMocks();
48        }
49    }
50
51    public void clearInlineMock(Object obj) {
52        InlineMockMaker inlineMockMaker = getInlineMockMaker();
53        if (inlineMockMaker != null) {
54            inlineMockMaker.clearMock(obj);
55        }
56    }
57}
58
Full Screen
copy
1package org.mockito;
2
3import org.mockito.exceptions.misusing.RedundantListenerException;
4import org.mockito.invocation.InvocationFactory;
5import org.mockito.listeners.MockitoListener;
6import org.mockito.plugins.MockitoPlugins;
7
8@Incubating
9public interface MockitoFramework {
10    @Incubating
11    MockitoFramework addListener(MockitoListener mockitoListener) throws RedundantListenerException;
12
13    @Incubating
14    void clearInlineMock(Object obj);
15
16    @Incubating
17    void clearInlineMocks();
18
19    @Incubating
20    InvocationFactory getInvocationFactory();
21
22    @Incubating
23    MockitoPlugins getPlugins();
24
25    @Incubating
26    MockitoFramework removeListener(MockitoListener mockitoListener);
27}
28
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.mockitousage.debugging;
6
7import org.assertj.core.api.Condition;
8import org.junit.After;
9import org.junit.Test;
10import org.junit.runner.JUnitCore;
11import org.mockito.Mockito;
12import org.mockito.MockitoFramework;
13import org.mockito.StateMaster;
14import org.mockito.exceptions.base.MockitoAssertionError;
15import org.mockito.internal.verification.api.VerificationData;
16import org.mockito.listeners.VerificationListener;
17import org.mockito.verification.VerificationEvent;
18import org.mockito.verification.VerificationMode;
19import org.mockitoutil.TestBase;
20
21import java.lang.reflect.Method;
22
23import static org.assertj.core.api.Assertions.assertThat;
24import static org.junit.Assert.fail;
25import static org.mockito.Mockito.*;
26
27public class VerificationListenerCallBackTest extends TestBase {
28
29    @After
30    public void clearListeners() {
31        new StateMaster().clearMockitoListeners();
32    }
33
34    @Test
35    public void should_call_single_listener_on_verify() throws NoSuchMethodException {
36        //given
37        RememberingListener listener = new RememberingListener();
38        MockitoFramework mockitoFramework = Mockito.framework();
39        mockitoFramework.addListener(listener);
40
41        Method invocationWanted = Foo.class.getDeclaredMethod("doSomething", String.class);
42        Foo foo = mock(Foo.class);
43
44        //when
45        VerificationMode never = never();
46        verify(foo, never).doSomething("");
47
48        //then
49        assertThat(listener).is(notifiedFor(foo, never, invocationWanted));
50    }
51
52    @Test
53    public void should_call_all_listeners_on_verify() throws NoSuchMethodException {
54        //given
55        RememberingListener listener1 = new RememberingListener();
56        RememberingListener2 listener2 = new RememberingListener2();
57        Mockito.framework().addListener(listener1).addListener(listener2);
58
59        Method invocationWanted = Foo.class.getDeclaredMethod("doSomething", String.class);
60        Foo foo = mock(Foo.class);
61
62        //when
63        VerificationMode never = never();
64        verify(foo, never).doSomething("");
65
66        //then
67        assertThat(listener1).is(notifiedFor(foo, never, invocationWanted));
68        assertThat(listener2).is(notifiedFor(foo, never, invocationWanted));
69    }
70
71    @Test
72    public void should_not_call_listener_when_verify_was_called_incorrectly() {
73        //when
74        VerificationListener listener = mock(VerificationListener.class);
75        framework().addListener(listener);
76        Foo foo = null;
77
78        try {
79            verify(foo).doSomething("");
80            fail("Exception expected.");
81        } catch (Exception e) {
82            //then
83            verify(listener, never()).onVerification(any(VerificationEvent.class));
84        }
85    }
86
87    @Test
88    public void should_notify_when_verification_throws_type_error() {
89        //given
90        RememberingListener listener = new RememberingListener();
91        MockitoFramework mockitoFramework = Mockito.framework();
92        mockitoFramework.addListener(listener);
93        Foo foo = mock(Foo.class);
94
95        //when
96        try {
97            verify(foo).doSomething("");
98            fail("Exception expected.");
99        } catch (Throwable e) {
100            //then
101            assertThat(listener.cause).isInstanceOf(MockitoAssertionError.class);
102        }
103    }
104
105    @Test
106    public void should_notify_when_verification_throws_runtime_exception() {
107        //given
108        RememberingListener listener = new RememberingListener();
109        MockitoFramework mockitoFramework = Mockito.framework();
110        mockitoFramework.addListener(listener);
111        Foo foo = mock(Foo.class);
112
113        //when
114        try {
115            verify(foo, new RuntimeExceptionVerificationMode()).doSomething("");
116            fail("Exception expected.");
117        } catch (Throwable e) {
118            //then
119            assertThat(listener.cause).isInstanceOf(RuntimeException.class);
120        }
121    }
122
123    @Test
124    public void should_call_verification_listeners() {
125        //given
126        RememberingListener listener = new RememberingListener();
127        MockitoFramework mockitoFramework = Mockito.framework();
128        mockitoFramework.addListener(listener);
129        JUnitCore runner = new JUnitCore();
130
131        //when
132        runner.run(VerificationListenerSample.class);
133
134        //then
135        assertThat(listener.mock).isNotNull();
136        assertThat(listener.mode).isEqualToComparingFieldByField(times(1));
137    }
138
139    public static class VerificationListenerSample {
140        @Test
141        public void verificationTest() {
142            Foo foo = mock(Foo.class);
143            foo.doSomething("");
144            verify(foo).doSomething("");
145        }
146    }
147
148    private static class RememberingListener implements VerificationListener {
149        Object mock;
150        VerificationMode mode;
151        VerificationData data;
152        Throwable cause;
153
154        @Override
155        public void onVerification(VerificationEvent verificationEvent) {
156            this.mock = verificationEvent.getMock();
157            this.mode = verificationEvent.getMode();
158            this.data = verificationEvent.getData();
159            this.cause = verificationEvent.getVerificationError();
160        }
161    }
162
163    private static class RememberingListener2 extends RememberingListener {
164
165    }
166
167    private static Condition<RememberingListener> notifiedFor(final Object mock, final VerificationMode mode, final Method wantedMethod) {
168        return new Condition<RememberingListener>() {
169            public boolean matches(RememberingListener listener) {
170                assertThat(listener.mock).isEqualTo(mock);
171                assertThat(listener.mode).isEqualTo(mode);
172                assertThat(listener.data.getTarget().getInvocation().getMethod()).isEqualTo(wantedMethod);
173
174                return true;
175            }
176        };
177    }
178
179    private static class RuntimeExceptionVerificationMode implements VerificationMode {
180        @Override
181        public void verify(VerificationData data) {
182            throw new RuntimeException();
183        }
184
185        @Override
186        public VerificationMode description(String description) {
187            return null;
188        }
189    }
190}
191
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 MockitoFramework

    No relevant questions found for this class 😞

Most used methods in MockitoFramework

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)