/
DefaultMockitoFrameworkTest.java
132 lines (107 loc) · 4.36 KB
/
DefaultMockitoFrameworkTest.java
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
/*
* 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.mockito.Mockito.*;
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 clears_all_mocks() {
List list = mock(List.class);
assertTrue(mockingDetails(list).isMock());
framework.clearAllMocks();
if (Plugins.getMockMaker() instanceof InlineMockMaker) {
assertFalse(mockingDetails(list).isMock());
}
}
private static class MyListener implements MockitoListener {}
}