1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.easymock.tests2; |
18 |
|
|
19 |
|
import static org.easymock.EasyMock.*; |
20 |
|
import static org.junit.Assert.*; |
21 |
|
|
22 |
|
import org.easymock.IAnswer; |
23 |
|
import org.easymock.tests.IMethods; |
24 |
|
import org.junit.Before; |
25 |
|
import org.junit.Test; |
26 |
|
|
27 |
|
|
28 |
|
@author |
29 |
|
|
|
|
| 95,9% |
Uncovered Elements: 3 (73) |
Complexity: 16 |
Complexity Density: 0,27 |
|
30 |
|
public class AnswerTest { |
31 |
|
|
32 |
|
private IMethods mock; |
33 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
34 |
12
|
@Before... |
35 |
|
public void setUp() { |
36 |
12
|
mock = createMock(IMethods.class); |
37 |
|
} |
38 |
|
|
|
|
| 91,7% |
Uncovered Elements: 1 (12) |
Complexity: 2 |
Complexity Density: 0,17 |
1
PASS
|
|
39 |
2
|
@Test... |
40 |
|
public void answer() { |
41 |
2
|
final IAnswer<Object> firstAnswer = new IAnswer<Object>() { |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
42 |
2
|
public Object answer() {... |
43 |
2
|
assertArrayEquals(new Object[] { 1, "2", "3" }, getCurrentArguments()); |
44 |
2
|
return "Call answered"; |
45 |
|
} |
46 |
|
}; |
47 |
|
|
48 |
2
|
final IAnswer<Object> secondAnswer = new IAnswer<Object>() { |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
49 |
2
|
public Object answer() {... |
50 |
2
|
assertArrayEquals(new Object[] { 1, "2", "3" }, getCurrentArguments()); |
51 |
2
|
throw new IllegalStateException("Call answered"); |
52 |
|
} |
53 |
|
}; |
54 |
|
|
55 |
2
|
expect(mock.threeArgumentMethod(1, "2", "3")).andAnswer(firstAnswer).andReturn("Second call") |
56 |
|
.andAnswer(secondAnswer).andReturn("Fourth call"); |
57 |
|
|
58 |
2
|
replay(mock); |
59 |
|
|
60 |
2
|
assertEquals("Call answered", mock.threeArgumentMethod(1, "2", "3")); |
61 |
2
|
assertEquals("Second call", mock.threeArgumentMethod(1, "2", "3")); |
62 |
2
|
try { |
63 |
2
|
mock.threeArgumentMethod(1, "2", "3"); |
64 |
0
|
fail(); |
65 |
|
} catch (final IllegalStateException expected) { |
66 |
2
|
assertEquals("Call answered", expected.getMessage()); |
67 |
|
} |
68 |
2
|
assertEquals("Fourth call", mock.threeArgumentMethod(1, "2", "3")); |
69 |
|
|
70 |
2
|
verify(mock); |
71 |
|
} |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0,09 |
1
PASS
|
|
73 |
2
|
@Test... |
74 |
|
public void stubAnswer() { |
75 |
2
|
final IAnswer<Object> firstAnswer = new IAnswer<Object>() { |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
76 |
6
|
public Object answer() {... |
77 |
6
|
assertArrayEquals(new Object[] { 1, "2", "3" }, getCurrentArguments()); |
78 |
6
|
return "Call answered"; |
79 |
|
} |
80 |
|
}; |
81 |
|
|
82 |
2
|
final IAnswer<Object> secondAnswer = new IAnswer<Object>() { |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
83 |
2
|
public Object answer() {... |
84 |
2
|
assertArrayEquals(new Object[] { 1, "2", "4" }, getCurrentArguments()); |
85 |
2
|
return "Call answered"; |
86 |
|
} |
87 |
|
}; |
88 |
|
|
89 |
2
|
expect(mock.threeArgumentMethod(1, "2", "3")).andReturn(42).andStubAnswer(firstAnswer); |
90 |
2
|
expect(mock.threeArgumentMethod(1, "2", "4")).andStubAnswer(secondAnswer); |
91 |
|
|
92 |
2
|
replay(mock); |
93 |
|
|
94 |
2
|
assertEquals(42, mock.threeArgumentMethod(1, "2", "3")); |
95 |
2
|
assertEquals("Call answered", mock.threeArgumentMethod(1, "2", "3")); |
96 |
2
|
assertEquals("Call answered", mock.threeArgumentMethod(1, "2", "4")); |
97 |
2
|
assertEquals("Call answered", mock.threeArgumentMethod(1, "2", "3")); |
98 |
2
|
assertEquals("Call answered", mock.threeArgumentMethod(1, "2", "3")); |
99 |
|
|
100 |
2
|
verify(mock); |
101 |
|
} |
102 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0,5 |
1
PASS
|
|
103 |
2
|
@Test... |
104 |
|
public void nullAnswerNotAllowed() { |
105 |
2
|
try { |
106 |
2
|
expect(mock.threeArgumentMethod(1, "2", "3")).andAnswer(null); |
107 |
0
|
fail(); |
108 |
|
} catch (final NullPointerException expected) { |
109 |
2
|
assertEquals("answer object must not be null", expected.getMessage()); |
110 |
|
} |
111 |
|
} |
112 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0,5 |
1
PASS
|
|
113 |
2
|
@Test... |
114 |
|
public void nullStubAnswerNotAllowed() { |
115 |
2
|
try { |
116 |
2
|
expect(mock.threeArgumentMethod(1, "2", "3")).andStubAnswer(null); |
117 |
0
|
fail(); |
118 |
|
} catch (final NullPointerException expected) { |
119 |
2
|
assertEquals("answer object must not be null", expected.getMessage()); |
120 |
|
} |
121 |
|
} |
122 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
123 |
|
public static class A { |
124 |
|
} |
125 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
126 |
|
public static class B extends A { |
127 |
|
} |
128 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
129 |
|
public static interface C { |
130 |
|
A foo(); |
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0,11 |
1
PASS
|
|
133 |
2
|
@Test... |
134 |
|
public void testGenericityFlexibility() { |
135 |
|
|
136 |
2
|
final C c = createMock(C.class); |
137 |
2
|
final B b = new B(); |
138 |
|
|
139 |
2
|
final IAnswer<B> answer = new IAnswer<B>() { |
140 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
141 |
4
|
public B answer() throws Throwable {... |
142 |
4
|
return b; |
143 |
|
} |
144 |
|
|
145 |
|
}; |
146 |
|
|
147 |
2
|
expect(c.foo()).andAnswer(answer); |
148 |
2
|
expect(c.foo()).andStubAnswer(answer); |
149 |
|
|
150 |
2
|
replay(c); |
151 |
2
|
assertSame(b, c.foo()); |
152 |
2
|
assertSame(b, c.foo()); |
153 |
2
|
verify(c); |
154 |
|
} |
155 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0,14 |
1
PASS
|
|
156 |
2
|
@Test... |
157 |
|
public void answerOnVoidMethod() { |
158 |
2
|
final String[] array = new String[] { "a" }; |
159 |
2
|
mock.arrayMethod(array); |
160 |
2
|
expectLastCall().andAnswer(new IAnswer<Object>() { |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
161 |
2
|
public Object answer() throws Throwable {... |
162 |
2
|
final String[] s = (String[]) getCurrentArguments()[0]; |
163 |
2
|
s[0] = "b"; |
164 |
2
|
return null; |
165 |
|
} |
166 |
|
}); |
167 |
2
|
replay(mock); |
168 |
2
|
mock.arrayMethod(array); |
169 |
2
|
verify(mock); |
170 |
|
|
171 |
2
|
assertEquals("b", array[0]); |
172 |
|
} |
173 |
|
} |