summaryrefslogtreecommitdiff
path: root/src/test/java/org/mockito/internal/framework/DefaultMockitoFrameworkTest.java
blob: ae21488c93f26a87142eae7656948da96c82a950 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (c) 2017 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package org.mockito.internal.framework;

import org.junit.After;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.MockSettings;
import org.mockito.StateMaster;
import org.mockito.exceptions.misusing.RedundantListenerException;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.listeners.MockCreationListener;
import org.mockito.listeners.MockitoListener;
import org.mockito.mock.MockCreationSettings;
import org.mockito.plugins.InlineMockMaker;
import org.mockitoutil.TestBase;

import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockingDetails;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.withSettings;
import static org.mockitoutil.ThrowableAssert.assertThat;

public class DefaultMockitoFrameworkTest extends TestBase {

    private DefaultMockitoFramework framework = new DefaultMockitoFramework();

    @After public void clearListeners() {
        new StateMaster().clearMockitoListeners();
    }

    @Test(expected = IllegalArgumentException.class)
    public void prevents_adding_null_listener() {
        framework.addListener(null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void prevents_removing_null_listener() {
        framework.removeListener(null);
    }

    @Test
    public void ok_to_remove_unknown_listener() {
        //it is safe to remove listener that was not added before
        framework.removeListener(new MockitoListener() {});
    }

    @Test
    public void ok_to_remove_listener_multiple_times() {
        MockitoListener listener = new MockitoListener() {};

        //when
        framework.addListener(listener);

        //then it is ok to:
        framework.removeListener(listener);
        framework.removeListener(listener);
    }

    @Test
    public void adds_creation_listener() {
        //given creation listener is added
        MockCreationListener listener = mock(MockCreationListener.class);
        framework.addListener(listener);

        //when
        MockSettings settings = withSettings().name("my list");
        List mock = mock(List.class, settings);
        Set mock2 = mock(Set.class);

        //then
        verify(listener).onMockCreated(eq(mock), any(MockCreationSettings.class));
        verify(listener).onMockCreated(eq(mock2), any(MockCreationSettings.class));
        verifyNoMoreInteractions(listener);
    }

    @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
    @Test
    public void removes_creation_listener() {
        //given creation listener is added
        MockCreationListener listener = mock(MockCreationListener.class);
        framework.addListener(listener);

        //and hooked up correctly
        mock(List.class);
        verify(listener).onMockCreated(ArgumentMatchers.any(), any(MockCreationSettings.class));

        //when
        framework.removeListener(listener);
        mock(Set.class);

        //then
        verifyNoMoreInteractions(listener);
    }

    @Test public void prevents_duplicate_listeners_of_the_same_type() {
        //given creation listener is added
        framework.addListener(new MyListener());

        assertThat(new Runnable() {
            @Override
            public void run() {
                framework.addListener(new MyListener());
            }
        })  .throwsException(RedundantListenerException.class)
            .throwsMessage("\n" +
                    "Problems adding Mockito listener.\n" +
                    "Listener of type 'MyListener' has already been added and not removed.\n" +
                    "It indicates that previous listener was not removed according to the API.\n" +
                    "When you add a listener, don't forget to remove the listener afterwards:\n" +
                    "  Mockito.framework().removeListener(myListener);\n" +
                    "For more information, see the javadoc for RedundantListenerException class.");
    }

    @Test
    public void clearing_all_mocks_is_safe_regardless_of_mock_maker_type() {
        List mock = mock(List.class);

        //expect
        assertTrue(mockingDetails(mock).isMock());
        framework.clearInlineMocks();
    }

    @Test
    public void clears_all_mocks() {
        //clearing mocks only works with inline mocking
        assumeTrue(Plugins.getMockMaker() instanceof InlineMockMaker);

        //given
        List list1 = mock(List.class);
        assertTrue(mockingDetails(list1).isMock());
        List list2 = mock(List.class);
        assertTrue(mockingDetails(list2).isMock());

        //when
        framework.clearInlineMocks();

        //then
        assertFalse(mockingDetails(list1).isMock());
        assertFalse(mockingDetails(list2).isMock());
    }

    @Test
    public void clears_mock() {
        //clearing mocks only works with inline mocking
        assumeTrue(Plugins.getMockMaker() instanceof InlineMockMaker);

        //given
        List list1 = mock(List.class);
        assertTrue(mockingDetails(list1).isMock());
        List list2 = mock(List.class);
        assertTrue(mockingDetails(list2).isMock());

        //when
        framework.clearInlineMock(list1);

        //then
        assertFalse(mockingDetails(list1).isMock());
        assertTrue(mockingDetails(list2).isMock());
    }

    private static class MyListener implements MockitoListener {}
}