-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
StrictnessPerStubbingWithRunnerTest.java
81 lines (67 loc) · 2.78 KB
/
StrictnessPerStubbingWithRunnerTest.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
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.strictness;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.leniently;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;
import org.assertj.core.api.ThrowableAssert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.exceptions.misusing.PotentialStubbingProblem;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockitousage.IMethods;
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class StrictnessPerStubbingWithRunnerTest {
@Mock IMethods mock;
@Test
public void potential_stubbing_problem() {
// when
when(mock.simpleMethod("1")).thenReturn("1");
lenient().when(mock.differentMethod("2")).thenReturn("2");
// then on lenient stubbing, we can call it with different argument:
mock.differentMethod("200");
// but on strict stubbing, we cannot:
assertThatThrownBy(
new ThrowableAssert.ThrowingCallable() {
public void call() {
ProductionCode.simpleMethod(mock, "100");
}
})
.isInstanceOf(PotentialStubbingProblem.class);
// let's use the strict stubbing so that it is not reported as failure by the runner:
mock.simpleMethod("1");
}
@Test
public void unnecessary_stubbing() {
// this unnecessary stubbing is not flagged by the runner:
lenient().when(mock.differentMethod("2")).thenReturn("2");
}
@Test
public void bdd_potential_stubbing_problem() {
// when
given(mock.simpleMethod("1")).willReturn("1");
leniently().given(mock.differentMethod("2")).willReturn("2");
// then on lenient stubbing, we can call it with different argument:
mock.differentMethod("200");
// but on strict stubbing, we cannot:
assertThatThrownBy(
new ThrowableAssert.ThrowingCallable() {
public void call() {
ProductionCode.simpleMethod(mock, "100");
}
})
.isInstanceOf(PotentialStubbingProblem.class);
// let's use the strict stubbing so that it is not reported as failure by the runner:
mock.simpleMethod("1");
}
@Test
public void bdd_unnecessary_stubbing() {
// this unnecessary stubbing is not flagged by the runner:
leniently().given(mock.differentMethod("2")).willReturn("2");
}
}