How to use MockitoJUnitRunner class of org.mockito.junit package

Best Mockito code snippet using org.mockito.junit.MockitoJUnitRunner

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.runners;
6
7import java.lang.reflect.InvocationTargetException;
8import org.junit.runner.Description;
9import org.junit.runner.manipulation.Filter;
10import org.junit.runner.manipulation.NoTestsRemainException;
11import org.junit.runner.notification.RunNotifier;
12
13
14/**
15 * Runner moved to a new place see {@link org.mockito.junit.MockitoJUnitRunner}
16 *
17 * @deprecated Moved to {@link org.mockito.junit.MockitoJUnitRunner}, this class will be removed with Mockito 3
18 */
19@Deprecated
20public class MockitoJUnitRunner extends org.mockito.junit.MockitoJUnitRunner {
21
22    /**
23     * Silent runner moved to a new place see {@link org.mockito.junit.MockitoJUnitRunner.Silent}
24     *
25     * @deprecated Moved to {@link org.mockito.junit.MockitoJUnitRunner.Silent}, this class will be removed with Mockito 3
26     */
27    @Deprecated
28    public static class Silent extends MockitoJUnitRunner {
29        public Silent(Class<?> klass) throws InvocationTargetException {
30            super(klass);
31        }
32    }
33
34    /**
35     * Silent runner moved to a new place see {@link org.mockito.junit.MockitoJUnitRunner.Strict}
36     *
37     * @deprecated Moved to {@link org.mockito.junit.MockitoJUnitRunner.Strict}, this class will be removed with Mockito 3
38     */
39    @Deprecated
40    public static class Strict extends MockitoJUnitRunner {
41        public Strict(Class<?> klass) throws InvocationTargetException {
42            super(klass);
43        }
44    }
45
46    public MockitoJUnitRunner(Class<?> klass) throws InvocationTargetException {
47        super(klass);
48    }
49
50    @Deprecated
51    @Override
52    public void run(final RunNotifier notifier) {
53        super.run(notifier);
54    }
55
56    @Deprecated
57    @Override
58    public Description getDescription() {
59        return super.getDescription();
60    }
61
62    @Deprecated
63    public void filter(Filter filter) throws NoTestsRemainException {
64        super.filter(filter);
65    }
66}
67
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.junitrunner;
6
7import org.junit.Before;
8import org.junit.Rule;
9import org.junit.Test;
10import org.junit.runner.JUnitCore;
11import org.junit.runner.Result;
12import org.junit.runner.RunWith;
13import org.mockito.Mock;
14import org.mockito.exceptions.misusing.UnnecessaryStubbingException;
15import org.mockito.junit.MockitoJUnit;
16import org.mockito.junit.MockitoRule;
17import org.mockito.junit.MockitoJUnitRunner;
18import org.mockitousage.IMethods;
19import org.mockitoutil.JUnitResultAssert;
20import org.mockitoutil.TestBase;
21
22import static org.junit.Assert.assertEquals;
23import static org.mockito.Mockito.mock;
24import static org.mockito.Mockito.when;
25
26
27public class StrictRunnerTest extends TestBase {
28
29    JUnitCore runner = new JUnitCore();
30
31    @Test public void succeeds_when_all_stubs_were_used() {
32        //when
33        Result result = runner.run(
34                StubbingInConstructorUsed.class,
35                StubbingInBeforeUsed.class,
36                StubbingInTestUsed.class
37        );
38        //then
39        JUnitResultAssert.assertThat(result).isSuccessful();
40    }
41
42    @Test public void fails_when_stubs_were_not_used() {
43        Class[] tests = {StubbingInConstructorUnused.class,
44                StubbingInBeforeUnused.class,
45                StubbingInTestUnused.class};
46
47        //when
48        Result result = runner.run(tests);
49
50        //then
51        JUnitResultAssert.assertThat(result).fails(3, UnnecessaryStubbingException.class);
52    }
53
54    @Test public void does_not_report_unused_stubs_when_different_failure_is_present() {
55        //when
56        Result result = runner.run(WithUnrelatedAssertionFailure.class);
57
58        //then
59        JUnitResultAssert.assertThat(result).fails(1, MyAssertionError.class);
60    }
61
62    @Test public void runner_can_coexist_with_rule() {
63        //I don't believe that this scenario is useful
64        //I only wish that Mockito does not break awkwardly when both: runner & rule is used
65
66        //when
67        Result result = runner.run(RunnerAndRule.class);
68
69        //then
70        JUnitResultAssert.assertThat(result).fails(1, UnnecessaryStubbingException.class);
71    }
72
73    @Test public void runner_in_multi_threaded_tests() {
74        //when
75        Result result = runner.run(StubUsedFromDifferentThread.class);
76
77        //then
78        JUnitResultAssert.assertThat(result).isSuccessful();
79    }
80
81    @RunWith(MockitoJUnitRunner.class)
82    public static class StubbingInConstructorUsed extends StubbingInConstructorUnused {
83        @Test public void test() {
84            assertEquals("1", mock.simpleMethod(1));
85        }
86    }
87
88    @RunWith(MockitoJUnitRunner.Strict.class) //using Strict to make sure it does the right thing
89    public static class StubbingInConstructorUnused {
90        IMethods mock = when(mock(IMethods.class).simpleMethod(1)).thenReturn("1").getMock();
91        @Test public void dummy() {}
92    }
93
94    @RunWith(MockitoJUnitRunner.class)
95    public static class StubbingInBeforeUsed extends StubbingInBeforeUnused {
96        @Test public void test() {
97            assertEquals("1", mock.simpleMethod(1));
98        }
99    }
100
101    @RunWith(MockitoJUnitRunner.class)
102    public static class StubbingInBeforeUnused {
103        @Mock IMethods mock;
104        @Before public void before() {
105            when(mock.simpleMethod(1)).thenReturn("1");
106        }
107        @Test public void dummy() {}
108    }
109
110    @RunWith(MockitoJUnitRunner.class)
111    public static class StubbingInTestUsed {
112        @Test public void test() {
113            IMethods mock = mock(IMethods.class);
114            when(mock.simpleMethod(1)).thenReturn("1");
115            assertEquals("1", mock.simpleMethod(1));
116        }
117    }
118
119    @RunWith(MockitoJUnitRunner.class)
120    public static class StubbingInTestUnused {
121        @Test public void test() {
122            IMethods mock = mock(IMethods.class);
123            when(mock.simpleMethod(1)).thenReturn("1");
124            mock.simpleMethod(2); //different arg
125        }
126    }
127
128    private static class MyAssertionError extends AssertionError {}
129
130    @RunWith(MockitoJUnitRunner.class)
131    public static class WithUnrelatedAssertionFailure {
132
133        IMethods mock = mock(IMethods.class);
134        IMethods mock2 = mock(IMethods.class);
135
136        @Before public void before() {
137            when(mock2.simpleMethod("unused stubbing")).thenReturn("");
138        }
139
140        @Test public void passing_test() {
141            when(mock.simpleMethod(1)).thenReturn("1");
142            assertEquals("1", mock.simpleMethod(1));
143        }
144
145        @Test public void failing_test() {
146            throw new MyAssertionError();
147        }
148    }
149
150    @RunWith(MockitoJUnitRunner.class)
151    public static class RunnerAndRule {
152
153        public @Rule MockitoRule rule = MockitoJUnit.rule();
154        IMethods mock = mock(IMethods.class);
155
156        @Test public void passing_test() {
157            when(mock.simpleMethod(1)).thenReturn("1");
158            mock.simpleMethod(2);
159        }
160    }
161
162    @RunWith(MockitoJUnitRunner.class)
163    public static class StubUsedFromDifferentThread {
164
165        IMethods mock = mock(IMethods.class);
166
167        @Test public void passing_test() throws Exception {
168            //stubbing is done in main thread:
169            when(mock.simpleMethod(1)).thenReturn("1");
170
171            //stubbing is used in a different thread
172            //stubbing should not be reported as unused by the runner
173            Thread t = new Thread() {
174                public void run() {
175                    mock.simpleMethod(1);
176                }
177            };
178            t.start();
179            t.join();
180        }
181    }
182}
183
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 MockitoJUnitRunner

    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)