How to use initMocks method of org.mockito.internal.session.DefaultMockitoSessionBuilder class

Best Mockito code snippet using org.mockito.internal.session.DefaultMockitoSessionBuilder.initMocks

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright (c) 2018 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.session;
6
7import static org.junit.Assert.assertFalse;
8import static org.junit.Assert.assertNotNull;
9import static org.mockito.Mockito.when;
10import static org.mockito.quality.Strictness.WARN;
11
12import java.util.ArrayList;
13import java.util.List;
14import java.util.Set;
15
16import org.junit.After;
17import org.junit.Test;
18import org.mockito.Mock;
19import org.mockito.MockitoSession;
20import org.mockito.StateMaster;
21import org.mockito.exceptions.misusing.UnfinishedMockingSessionException;
22import org.mockito.quality.Strictness;
23import org.mockito.session.MockitoSessionLogger;
24import org.mockitoutil.ThrowableAssert;
25
26public class DefaultMockitoSessionBuilderTest {
27
28    @After
29    public void after() {
30        new StateMaster().clearMockitoListeners();
31    }
32
33    @Test
34    public void creates_sessions() {
35        // no configuration is legal
36        new DefaultMockitoSessionBuilder().startMocking().finishMocking();
37
38        // passing null to configuration is legal, default value will be used
39        new DefaultMockitoSessionBuilder().initMocks((Object) null).startMocking().finishMocking();
40        new DefaultMockitoSessionBuilder()
41                .initMocks((Object[]) null)
42                .startMocking()
43                .finishMocking();
44        new DefaultMockitoSessionBuilder()
45                .initMocks(null, null)
46                .strictness(null)
47                .startMocking()
48                .finishMocking();
49        new DefaultMockitoSessionBuilder().strictness(null).startMocking().finishMocking();
50
51        // happy path
52        new DefaultMockitoSessionBuilder().initMocks(this).startMocking().finishMocking();
53        new DefaultMockitoSessionBuilder().initMocks(new Object()).startMocking().finishMocking();
54        new DefaultMockitoSessionBuilder()
55                .strictness(Strictness.LENIENT)
56                .startMocking()
57                .finishMocking();
58    }
59
60    @Test
61    public void creates_sessions_for_multiple_test_class_instances_for_repeated_calls() {
62        TestClass testClass = new TestClass();
63        TestClass.NestedTestClass nestedTestClass = testClass.new NestedTestClass();
64
65        new DefaultMockitoSessionBuilder()
66                .initMocks(testClass)
67                .initMocks(nestedTestClass)
68                .startMocking()
69                .finishMocking();
70
71        assertNotNull(testClass.set);
72        assertNotNull(nestedTestClass.list);
73    }
74
75    @Test
76    public void creates_sessions_for_multiple_test_class_instances_for_varargs_call() {
77        TestClass testClass = new TestClass();
78        TestClass.NestedTestClass nestedTestClass = testClass.new NestedTestClass();
79
80        new DefaultMockitoSessionBuilder()
81                .initMocks(testClass, nestedTestClass)
82                .startMocking()
83                .finishMocking();
84
85        assertNotNull(testClass.set);
86        assertNotNull(nestedTestClass.list);
87    }
88
89    @Test
90    public void uses_logger_and_strictness() {
91        TestClass testClass = new TestClass();
92
93        final List<String> hints = new ArrayList<String>();
94        MockitoSession session =
95                new DefaultMockitoSessionBuilder()
96                        .initMocks(testClass)
97                        .strictness(WARN)
98                        .logger(
99                                new MockitoSessionLogger() {
100                                    @Override
101                                    public void log(String hint) {
102                                        hints.add(hint);
103                                    }
104                                })
105                        .startMocking();
106
107        when(testClass.set.add(1)).thenReturn(true);
108
109        session.finishMocking();
110
111        assertFalse(hints.isEmpty());
112    }
113
114    @Test
115    public void requires_finish_mocking() {
116        new DefaultMockitoSessionBuilder().startMocking();
117
118        ThrowableAssert.assertThat(
119                        new Runnable() {
120                            public void run() {
121                                new DefaultMockitoSessionBuilder().startMocking();
122                            }
123                        })
124                .throwsException(UnfinishedMockingSessionException.class);
125    }
126
127    @Test
128    public void auto_cleans_dirty_listeners() {
129        new DefaultMockitoSessionBuilder().startMocking();
130
131        ThrowableAssert.assertThat(
132                        new Runnable() {
133                            public void run() {
134                                new DefaultMockitoSessionBuilder().startMocking();
135                            }
136                        })
137                .throwsException(UnfinishedMockingSessionException.class);
138    }
139
140    class TestClass {
141
142        @Mock public Set<Object> set;
143
144        class NestedTestClass {
145            @Mock public List<Object> list;
146        }
147    }
148}
149
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 used method in DefaultMockitoSessionBuilder

Trigger initMocks code on LambdaTest Cloud Grid

Execute automation tests with initMocks on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

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