-
Notifications
You must be signed in to change notification settings - Fork 1
/
DetectingMisusedMatchersTest.java
executable file
·92 lines (75 loc) · 2.73 KB
/
DetectingMisusedMatchersTest.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
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.misuse;
import org.junit.After;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.exceptions.misusing.InvalidUseOfMatchersException;
import org.mockito.exceptions.misusing.UnfinishedVerificationException;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;
import java.util.Observer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import static org.mockito.Mockito.*;
public class DetectingMisusedMatchersTest extends TestBase {
class WithFinal {
final Object finalMethod(Object object) {
return null;
}
}
@Mock private WithFinal withFinal;
@After
public void resetState() {
super.resetState();
}
private void misplaced_anyObject_argument_matcher() {
anyObject();
}
private void misplaced_anyInt_argument_matcher() {
anyInt();
}
private void misplaced_anyBoolean_argument_matcher() {
anyBoolean();
}
@Test
public void should_fail_fast_when_argument_matchers_are_abused() {
misplaced_anyObject_argument_matcher();
try {
mock(IMethods.class);
fail();
} catch (InvalidUseOfMatchersException e) {
assertThat(e).hasMessageContaining("Misplaced or misused argument matcher");
}
}
@Test
public void should_report_argument_locations_when_argument_matchers_misused() {
try {
Observer observer = mock(Observer.class);
misplaced_anyInt_argument_matcher();
misplaced_anyObject_argument_matcher();
misplaced_anyBoolean_argument_matcher();
observer.update(null, null);
validateMockitoUsage();
fail();
} catch (InvalidUseOfMatchersException e) {
assertThat(e)
.hasMessageContaining("DetectingMisusedMatchersTest.misplaced_anyInt_argument_matcher")
.hasMessageContaining("DetectingMisusedMatchersTest.misplaced_anyObject_argument_matcher")
.hasMessageContaining("DetectingMisusedMatchersTest.misplaced_anyBoolean_argument_matcher");
}
}
@SuppressWarnings({"MockitoUsage", "CheckReturnValue"})
@Test
public void shouldSayUnfinishedVerificationButNotInvalidUseOfMatchers() {
assumeTrue("Does not apply for inline mocks", withFinal.getClass() != WithFinal.class);
verify(withFinal).finalMethod(anyObject());
try {
verify(withFinal);
fail();
} catch (UnfinishedVerificationException e) {}
}
}