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

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

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1package org.mockito;
2
3import org.mockito.internal.MockitoCore;
4import org.mockito.internal.creation.MockSettingsImpl;
5import org.mockito.internal.debugging.MockitoDebuggerImpl;
6import org.mockito.internal.framework.DefaultMockitoFramework;
7import org.mockito.internal.session.DefaultMockitoSessionBuilder;
8import org.mockito.internal.verification.VerificationModeFactory;
9import org.mockito.session.MockitoSessionBuilder;
10import org.mockito.stubbing.Answer;
11import org.mockito.stubbing.LenientStubber;
12import org.mockito.stubbing.OngoingStubbing;
13import org.mockito.stubbing.Stubber;
14import org.mockito.verification.After;
15import org.mockito.verification.Timeout;
16import org.mockito.verification.VerificationAfterDelay;
17import org.mockito.verification.VerificationMode;
18import org.mockito.verification.VerificationWithTimeout;
19
20public class Mockito extends ArgumentMatchers {
21    public static final Answer<Object> CALLS_REAL_METHODS = Answers.CALLS_REAL_METHODS;
22    static final MockitoCore MOCKITO_CORE = new MockitoCore();
23    public static final Answer<Object> RETURNS_DEEP_STUBS = Answers.RETURNS_DEEP_STUBS;
24    public static final Answer<Object> RETURNS_DEFAULTS = Answers.RETURNS_DEFAULTS;
25    public static final Answer<Object> RETURNS_MOCKS = Answers.RETURNS_MOCKS;
26    public static final Answer<Object> RETURNS_SELF = Answers.RETURNS_SELF;
27    public static final Answer<Object> RETURNS_SMART_NULLS = Answers.RETURNS_SMART_NULLS;
28
29    public static <T> T mock(Class<T> cls) {
30        return mock(cls, withSettings());
31    }
32
33    public static <T> T mock(Class<T> cls, String str) {
34        return mock(cls, withSettings().name(str).defaultAnswer(RETURNS_DEFAULTS));
35    }
36
37    public static MockingDetails mockingDetails(Object obj) {
38        return MOCKITO_CORE.mockingDetails(obj);
39    }
40
41    public static <T> T mock(Class<T> cls, Answer answer) {
42        return mock(cls, withSettings().defaultAnswer(answer));
43    }
44
45    public static <T> T mock(Class<T> cls, MockSettings mockSettings) {
46        return MOCKITO_CORE.mock(cls, mockSettings);
47    }
48
49    public static <T> T spy(T t) {
50        return MOCKITO_CORE.mock(t.getClass(), withSettings().spiedInstance(t).defaultAnswer(CALLS_REAL_METHODS));
51    }
52
53    @Incubating
54    public static <T> T spy(Class<T> cls) {
55        return MOCKITO_CORE.mock(cls, withSettings().useConstructor(new Object[0]).defaultAnswer(CALLS_REAL_METHODS));
56    }
57
58    public static <T> OngoingStubbing<T> when(T t) {
59        return MOCKITO_CORE.when(t);
60    }
61
62    public static <T> T verify(T t) {
63        return MOCKITO_CORE.verify(t, times(1));
64    }
65
66    public static <T> T verify(T t, VerificationMode verificationMode) {
67        return MOCKITO_CORE.verify(t, verificationMode);
68    }
69
70    public static <T> void reset(T... tArr) {
71        MOCKITO_CORE.reset(tArr);
72    }
73
74    public static <T> void clearInvocations(T... tArr) {
75        MOCKITO_CORE.clearInvocations(tArr);
76    }
77
78    public static void verifyNoMoreInteractions(Object... objArr) {
79        MOCKITO_CORE.verifyNoMoreInteractions(objArr);
80    }
81
82    public static void verifyZeroInteractions(Object... objArr) {
83        MOCKITO_CORE.verifyNoMoreInteractions(objArr);
84    }
85
86    public static Stubber doThrow(Throwable... thArr) {
87        return MOCKITO_CORE.stubber().doThrow(thArr);
88    }
89
90    public static Stubber doThrow(Class<? extends Throwable> cls) {
91        return MOCKITO_CORE.stubber().doThrow(cls);
92    }
93
94    public static Stubber doThrow(Class<? extends Throwable> cls, Class<? extends Throwable>... clsArr) {
95        return MOCKITO_CORE.stubber().doThrow(cls, clsArr);
96    }
97
98    public static Stubber doCallRealMethod() {
99        return MOCKITO_CORE.stubber().doCallRealMethod();
100    }
101
102    public static Stubber doAnswer(Answer answer) {
103        return MOCKITO_CORE.stubber().doAnswer(answer);
104    }
105
106    public static Stubber doNothing() {
107        return MOCKITO_CORE.stubber().doNothing();
108    }
109
110    public static Stubber doReturn(Object obj) {
111        return MOCKITO_CORE.stubber().doReturn(obj);
112    }
113
114    public static Stubber doReturn(Object obj, Object... objArr) {
115        return MOCKITO_CORE.stubber().doReturn(obj, objArr);
116    }
117
118    public static InOrder inOrder(Object... objArr) {
119        return MOCKITO_CORE.inOrder(objArr);
120    }
121
122    public static Object[] ignoreStubs(Object... objArr) {
123        return MOCKITO_CORE.ignoreStubs(objArr);
124    }
125
126    public static VerificationMode times(int i) {
127        return VerificationModeFactory.times(i);
128    }
129
130    public static VerificationMode never() {
131        return times(0);
132    }
133
134    public static VerificationMode atLeastOnce() {
135        return VerificationModeFactory.atLeastOnce();
136    }
137
138    public static VerificationMode atLeast(int i) {
139        return VerificationModeFactory.atLeast(i);
140    }
141
142    public static VerificationMode atMost(int i) {
143        return VerificationModeFactory.atMost(i);
144    }
145
146    public static VerificationMode calls(int i) {
147        return VerificationModeFactory.calls(i);
148    }
149
150    public static VerificationMode only() {
151        return VerificationModeFactory.only();
152    }
153
154    public static VerificationWithTimeout timeout(long j) {
155        return new Timeout(j, VerificationModeFactory.times(1));
156    }
157
158    public static VerificationAfterDelay after(long j) {
159        return new After(j, VerificationModeFactory.times(1));
160    }
161
162    public static void validateMockitoUsage() {
163        MOCKITO_CORE.validateMockitoUsage();
164    }
165
166    public static MockSettings withSettings() {
167        return new MockSettingsImpl().defaultAnswer(RETURNS_DEFAULTS);
168    }
169
170    public static VerificationMode description(String str) {
171        return times(1).description(str);
172    }
173
174    @Deprecated
175    static MockitoDebugger debug() {
176        return new MockitoDebuggerImpl();
177    }
178
179    @Incubating
180    public static MockitoFramework framework() {
181        return new DefaultMockitoFramework();
182    }
183
184    @Incubating
185    public static MockitoSessionBuilder mockitoSession() {
186        return new DefaultMockitoSessionBuilder();
187    }
188
189    @Incubating
190    public static LenientStubber lenient() {
191        return MOCKITO_CORE.lenient();
192    }
193}
194
Full Screen
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 org.mockito.MockitoSession;
8import org.mockito.internal.framework.DefaultMockitoSession;
9import org.mockito.internal.util.ConsoleMockitoLogger;
10import org.mockito.internal.util.MockitoLogger;
11import org.mockito.quality.Strictness;
12import org.mockito.session.MockitoSessionBuilder;
13import org.mockito.session.MockitoSessionLogger;
14
15import java.util.ArrayList;
16import java.util.List;
17
18import static java.util.Collections.emptyList;
19
20public class DefaultMockitoSessionBuilder implements MockitoSessionBuilder {
21
22    private List<Object> testClassInstances = new ArrayList<Object>();
23    private String name;
24    private Strictness strictness;
25    private MockitoSessionLogger logger;
26
27    @Override
28    public MockitoSessionBuilder initMocks(Object testClassInstance) {
29        if (testClassInstance != null) {
30            this.testClassInstances.add(testClassInstance);
31        }
32        return this;
33    }
34
35    @Override
36    public MockitoSessionBuilder initMocks(Object... testClassInstances) {
37        if (testClassInstances != null) {
38            for (Object instance : testClassInstances) {
39                initMocks(instance);
40            }
41        }
42        return this;
43    }
44
45    @Override
46    public MockitoSessionBuilder name(String name) {
47        this.name = name;
48        return this;
49    }
50
51    @Override
52    public MockitoSessionBuilder strictness(Strictness strictness) {
53        this.strictness = strictness;
54        return this;
55    }
56
57    @Override
58    public MockitoSessionBuilder logger(MockitoSessionLogger logger) {
59        this.logger = logger;
60        return this;
61    }
62
63    @Override
64    public MockitoSession startMocking() {
65        //Configure default values
66        List<Object> effectiveTestClassInstances;
67        String effectiveName;
68        if (testClassInstances.isEmpty()) {
69            effectiveTestClassInstances = emptyList();
70            effectiveName = this.name == null ? "<Unnamed Session>" : this.name;
71        } else {
72            effectiveTestClassInstances = new ArrayList<Object>(testClassInstances);
73            Object lastTestClassInstance = testClassInstances.get(testClassInstances.size() - 1);
74            effectiveName = this.name == null ? lastTestClassInstance.getClass().getName() : this.name;
75        }
76        Strictness effectiveStrictness = this.strictness == null ? Strictness.STRICT_STUBS : this.strictness;
77        MockitoLogger logger = this.logger == null ? new ConsoleMockitoLogger() : new MockitoLoggerAdapter(this.logger);
78        return new DefaultMockitoSession(effectiveTestClassInstances, effectiveName, effectiveStrictness, logger);
79    }
80}
81
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 name code on LambdaTest Cloud Grid

Execute automation tests with name 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