forked from mockito/mockito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArgumentMatchingToolTest.java
141 lines (114 loc) · 4.3 KB
/
ArgumentMatchingToolTest.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
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.verification.argumentmatching;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.internal.matchers.ContainsExtraTypeInfo;
import org.mockito.internal.matchers.Equals;
import org.mockitoutil.TestBase;
@SuppressWarnings({"unchecked", "serial"})
public class ArgumentMatchingToolTest extends TestBase {
@Test
public void shouldNotFindAnySuspiciousMatchersWhenNumberOfArgumentsDoesntMatch() {
// given
List<ArgumentMatcher> matchers = (List) Arrays.asList(new Equals(1));
// when
Integer[] suspicious =
ArgumentMatchingTool.getSuspiciouslyNotMatchingArgsIndexes(
matchers, new Object[] {10, 20});
// then
assertEquals(0, suspicious.length);
}
@Test
public void shouldNotFindAnySuspiciousMatchersWhenArgumentsMatch() {
// given
List<ArgumentMatcher> matchers = (List) Arrays.asList(new Equals(10), new Equals(20));
// when
Integer[] suspicious =
ArgumentMatchingTool.getSuspiciouslyNotMatchingArgsIndexes(
matchers, new Object[] {10, 20});
// then
assertEquals(0, suspicious.length);
}
@Test
public void shouldFindSuspiciousMatchers() {
// given
Equals matcherInt20 = new Equals(20);
Long longPretendingAnInt = 20L;
// when
List<ArgumentMatcher> matchers = (List) Arrays.asList(new Equals(10), matcherInt20);
Integer[] suspicious =
ArgumentMatchingTool.getSuspiciouslyNotMatchingArgsIndexes(
matchers, new Object[] {10, longPretendingAnInt});
// then
assertEquals(1, suspicious.length);
assertEquals(new Integer(1), suspicious[0]);
}
@Test
public void shouldNotFindSuspiciousMatchersWhenTypesAreTheSame() {
// given
Equals matcherWithBadDescription =
new Equals(20) {
public String toString() {
return "10";
}
};
Integer argument = 10;
// when
Integer[] suspicious =
ArgumentMatchingTool.getSuspiciouslyNotMatchingArgsIndexes(
(List) Arrays.asList(matcherWithBadDescription), new Object[] {argument});
// then
assertEquals(0, suspicious.length);
}
@Test
public void shouldWorkFineWhenGivenArgIsNull() {
// when
Integer[] suspicious =
ArgumentMatchingTool.getSuspiciouslyNotMatchingArgsIndexes(
(List) Arrays.asList(new Equals(20)), new Object[] {null});
// then
assertEquals(0, suspicious.length);
}
@Test
@SuppressWarnings("rawtypes")
public void shouldUseMatchersSafely() {
// This matcher is evil cause typeMatches(Object) returns true for every passed type but
// matches(T)
// method accepts only Strings. When a Integer is passed (thru the matches(Object) bridge
// method ) a
// ClassCastException will be thrown.
class StringMatcher implements ArgumentMatcher<String>, ContainsExtraTypeInfo {
@Override
public boolean matches(String item) {
return true; // in this test we never get here
}
@Override
public String toStringWithType(boolean useFullyQualifiedClassName) {
return "";
}
@Override
public boolean typeMatches(Object target) {
return true;
}
@Override
public Class getWantedClass() {
return String.class;
}
}
// given
List<ArgumentMatcher> matchers = (List) singletonList(new StringMatcher());
// when
Integer[] suspicious =
ArgumentMatchingTool.getSuspiciouslyNotMatchingArgsIndexes(
matchers, new Object[] {10});
// then
assertEquals(0, suspicious.length);
}
}