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

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

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 org.junit.After;
8import org.junit.Test;
9import org.mockito.Mock;
10import org.mockito.MockitoSession;
11import org.mockito.StateMaster;
12import org.mockito.exceptions.misusing.UnfinishedMockingSessionException;
13import org.mockito.quality.Strictness;
14import org.mockito.session.MockitoSessionLogger;
15import org.mockitoutil.ThrowableAssert;
16
17import java.util.ArrayList;
18import java.util.List;
19import java.util.Set;
20
21import static org.junit.Assert.assertFalse;
22import static org.junit.Assert.assertNotNull;
23import static org.mockito.Mockito.when;
24import static org.mockito.quality.Strictness.WARN;
25
26public class DefaultMockitoSessionBuilderTest {
27
28    @After public void after() {
29        new StateMaster().clearMockitoListeners();
30    }
31
32    @Test public void creates_sessions() {
33        //no configuration is legal
34        new DefaultMockitoSessionBuilder().startMocking().finishMocking();
35
36        //passing null to configuration is legal, default value will be used
37        new DefaultMockitoSessionBuilder().initMocks((Object) null).startMocking().finishMocking();
38        new DefaultMockitoSessionBuilder().initMocks((Object[]) null).startMocking().finishMocking();
39        new DefaultMockitoSessionBuilder().initMocks(null, null).strictness(null).startMocking().finishMocking();
40        new DefaultMockitoSessionBuilder().strictness(null).startMocking().finishMocking();
41
42        //happy path
43        new DefaultMockitoSessionBuilder().initMocks(this).startMocking().finishMocking();
44        new DefaultMockitoSessionBuilder().initMocks(new Object()).startMocking().finishMocking();
45        new DefaultMockitoSessionBuilder().strictness(Strictness.LENIENT).startMocking().finishMocking();
46    }
47
48    @Test public void creates_sessions_for_multiple_test_class_instances_for_repeated_calls() {
49        TestClass testClass = new TestClass();
50        TestClass.NestedTestClass nestedTestClass = testClass.new NestedTestClass();
51
52        new DefaultMockitoSessionBuilder().initMocks(testClass).initMocks(nestedTestClass).startMocking().finishMocking();
53
54        assertNotNull(testClass.set);
55        assertNotNull(nestedTestClass.list);
56    }
57
58    @Test public void creates_sessions_for_multiple_test_class_instances_for_varargs_call() {
59        TestClass testClass = new TestClass();
60        TestClass.NestedTestClass nestedTestClass = testClass.new NestedTestClass();
61
62        new DefaultMockitoSessionBuilder().initMocks(testClass, nestedTestClass).startMocking().finishMocking();
63
64        assertNotNull(testClass.set);
65        assertNotNull(nestedTestClass.list);
66    }
67
68    @Test public void uses_logger_and_strictness() {
69        TestClass testClass = new TestClass();
70
71        final List<String> hints = new ArrayList<String>();
72        MockitoSession session = new DefaultMockitoSessionBuilder()
73            .initMocks(testClass)
74            .strictness(WARN)
75            .logger(new MockitoSessionLogger() {
76                @Override
77                public void log(String hint) {
78                    hints.add(hint);
79                }
80            })
81            .startMocking();
82
83        when(testClass.set.add(1)).thenReturn(true);
84
85        session.finishMocking();
86
87        assertFalse(hints.isEmpty());
88    }
89
90    @Test public void requires_finish_mocking() {
91        new DefaultMockitoSessionBuilder().startMocking();
92
93        ThrowableAssert.assertThat(new Runnable() {
94            public void run() {
95                new DefaultMockitoSessionBuilder().startMocking();
96            }
97        }).throwsException(UnfinishedMockingSessionException.class);
98    }
99
100    class TestClass {
101
102        @Mock public Set<Object> set;
103
104        class NestedTestClass {
105            @Mock public List<Object> list;
106        }
107    }
108}
109
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 logger code on LambdaTest Cloud Grid

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