forked from mockito/mockito
/
StrictnessTest.java
215 lines (170 loc) · 7.36 KB
/
StrictnessTest.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (c) 2018 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.exceptions.misusing.UnnecessaryStubbingException;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
/**
* Test that runs the inner test using a launcher {@see #invokeTestClassAndRetrieveMethodResult}.
* We then assert on the actual test run output, to see if test actually failed as a result
* of our extension.
*/
class StrictnessTest {
@MockitoSettings(strictness = Strictness.STRICT_STUBS)
static class StrictStubs {
@Mock
private Function<Integer, String> rootMock;
@Test
void should_throw_an_exception_on_strict_stubs() {
Mockito.when(rootMock.apply(10)).thenReturn("Foo");
}
}
@Test
void session_checks_for_strict_stubs() {
TestExecutionResult result = invokeTestClassAndRetrieveMethodResult(StrictStubs.class);
assertThat(result.getStatus()).isEqualTo(TestExecutionResult.Status.FAILED);
assertThat(result.getThrowable().get()).isInstanceOf(UnnecessaryStubbingException.class);
}
static class MyAssertionError extends AssertionError {
}
@MockitoSettings(strictness = Strictness.STRICT_STUBS)
static class StrictStubsNotReportedOnTestFailure {
@Mock
private Function<Integer, String> rootMock;
@Test
void should_not_throw_exception_on_strict_stubs_because_of_test_failure() {
Mockito.when(rootMock.apply(10)).thenReturn("Foo");
throw new MyAssertionError();
}
}
@Test
void session_does_not_check_for_strict_stubs_on_test_failure() {
TestExecutionResult result = invokeTestClassAndRetrieveMethodResult(StrictStubsNotReportedOnTestFailure.class);
assertThat(result.getStatus()).isEqualTo(TestExecutionResult.Status.FAILED);
Throwable throwable = result.getThrowable().get();
assertThat(throwable).isInstanceOf(MyAssertionError.class);
assertThat(throwable.getSuppressed()).isEmpty();
}
@MockitoSettings(strictness = Strictness.STRICT_STUBS)
static class ConfiguredStrictStubs {
@Nested
class NestedStrictStubs {
@Mock
private Function<Integer, String> rootMock;
@Test
void should_throw_an_exception_on_strict_stubs_in_a_nested_class() {
Mockito.when(rootMock.apply(10)).thenReturn("Foo");
}
}
}
@Test
void session_can_retrieve_strictness_from_parent_class() {
TestExecutionResult result = invokeTestClassAndRetrieveMethodResult(ConfiguredStrictStubs.class);
assertThat(result.getStatus()).isEqualTo(TestExecutionResult.Status.FAILED);
assertThat(result.getThrowable().get()).isInstanceOf(UnnecessaryStubbingException.class);
}
@MockitoSettings(strictness = Strictness.STRICT_STUBS)
static class ParentConfiguredStrictStubs {
@Nested
@MockitoSettings(strictness = Strictness.WARN)
class ChildConfiguredWarnStubs {
@Mock
private Function<Integer, String> rootMock;
@Test
void should_throw_an_exception_on_strict_stubs_in_a_nested_class() {
Mockito.when(rootMock.apply(10)).thenReturn("Foo");
}
}
}
@Test
void session_retrieves_closest_strictness_configuration() {
TestExecutionResult result = invokeTestClassAndRetrieveMethodResult(ParentConfiguredStrictStubs.class);
assertThat(result.getStatus()).isEqualTo(TestExecutionResult.Status.SUCCESSFUL);
}
@ExtendWith(MockitoExtension.class)
static class ByDefaultUsesStrictStubs {
@Mock
private Function<Integer, String> rootMock;
@Test
void should_throw_an_exception_on_strict_stubs_configured_by_default() {
Mockito.when(rootMock.apply(10)).thenReturn("Foo");
}
}
@Test
void by_default_configures_strict_stubs_in_runner() {
TestExecutionResult result = invokeTestClassAndRetrieveMethodResult(ByDefaultUsesStrictStubs.class);
assertThat(result.getStatus()).isEqualTo(TestExecutionResult.Status.FAILED);
assertThat(result.getThrowable().get()).isInstanceOf(UnnecessaryStubbingException.class);
}
@MockitoSettings(strictness = Strictness.WARN)
static class BaseWarnStubs {}
static class InheritedWarnStubs extends BaseWarnStubs {
@Mock
private Function<Integer, String> rootMock;
@Test
void should_execute_successfully_on_warn_stubs_inherited_from_base_class() {
Mockito.when(rootMock.apply(10)).thenReturn("Foo");
}
}
@Test
void inherits_strictness_from_base_class() {
TestExecutionResult result = invokeTestClassAndRetrieveMethodResult(InheritedWarnStubs.class);
assertThat(result.getStatus()).isEqualTo(TestExecutionResult.Status.SUCCESSFUL);
}
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
static class LenientMockitoSettings {
@Mock
private Predicate<String> rootMock;
@Test
void should_not_throw_on_potential_stubbing_issue() {
Mockito.doReturn(true).when(rootMock).test("Foo");
ProductionCode.simpleMethod(rootMock, "Bar");
}
}
@Test
void use_strictness_from_settings_annotation() {
TestExecutionResult result = invokeTestClassAndRetrieveMethodResult(LenientMockitoSettings.class);
assertThat(result.getThrowable()).isEqualTo(Optional.empty());
assertThat(result.getStatus()).isEqualTo(TestExecutionResult.Status.SUCCESSFUL);
}
private TestExecutionResult invokeTestClassAndRetrieveMethodResult(Class<?> clazz) {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(
selectClass(clazz)
)
.build();
Launcher launcher = LauncherFactory.create();
final TestExecutionResult[] result = new TestExecutionResult[1];
launcher.registerTestExecutionListeners(new TestExecutionListener() {
@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
if (testIdentifier.getDisplayName().endsWith("()")) {
result[0] = testExecutionResult;
}
}
});
launcher.execute(request);
return result[0];
}
}