/
JUnitRule.java
79 lines (71 loc) · 2.6 KB
/
JUnitRule.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
/*
* Copyright (c) 2018 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.junit;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.MockitoSession;
import org.mockito.internal.session.MockitoSessionLoggerAdapter;
import org.mockito.junit.MockitoRule;
import org.mockito.plugins.MockitoLogger;
import org.mockito.quality.Strictness;
/**
* Internal implementation.
*/
public class JUnitRule implements MockitoRule {
private final MockitoLogger logger;
private Strictness strictness;
private MockitoSession session;
/**
* @param strictness how strict mocking / stubbing is concerned
*/
public JUnitRule(MockitoLogger logger, Strictness strictness) {
this.logger = logger;
this.strictness = strictness;
}
@Override
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
return new Statement() {
public void evaluate() throws Throwable {
if (session == null) {
session = Mockito.mockitoSession()
.name(target.getClass().getSimpleName() + "." + method.getName())
.strictness(strictness)
.logger(new MockitoSessionLoggerAdapter(logger))
.initMocks(target)
.startMocking();
} else {
MockitoAnnotations.initMocks(target);
}
Throwable testFailure = evaluateSafely(base);
session.finishMocking(testFailure);
if (testFailure != null) {
throw testFailure;
}
}
private Throwable evaluateSafely(Statement base) {
try {
base.evaluate();
return null;
} catch (Throwable throwable) {
return throwable;
}
}
};
}
public MockitoRule silent() {
return strictness(Strictness.LENIENT);
}
public MockitoRule strictness(Strictness strictness) {
this.strictness = strictness;
// session is null when this method is called during initialization of
// the @Rule field of the test class
if (session != null) {
session.setStrictness(strictness);
}
return this;
}
}