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 java.lang.reflect.Method; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.List; |
25 |
|
|
26 |
|
import org.easymock.ConstructorArgs; |
27 |
|
import org.easymock.IMocksControl; |
28 |
|
import org.junit.Test; |
29 |
|
|
30 |
|
|
31 |
|
@author |
32 |
|
|
|
|
| 94,4% |
Uncovered Elements: 4 (71) |
Complexity: 13 |
Complexity Density: 0,21 |
|
33 |
|
public class MocksControlTest { |
34 |
|
|
|
|
| 44,4% |
Uncovered Elements: 5 (9) |
Complexity: 4 |
Complexity Density: 0,8 |
|
35 |
|
public static class A { |
36 |
|
int i = 1; |
37 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
38 |
18
|
public A(final int i) {... |
39 |
18
|
this.i = i; |
40 |
|
} |
41 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
42 |
8
|
public int foo() {... |
43 |
8
|
return bar(); |
44 |
|
} |
45 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
46 |
0
|
public int bar() {... |
47 |
0
|
return i; |
48 |
|
} |
49 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
50 |
0
|
public boolean add(final int i) {... |
51 |
0
|
this.i += i; |
52 |
0
|
return true; |
53 |
|
} |
54 |
|
} |
55 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
1
PASS
|
|
56 |
2
|
@Test... |
57 |
|
public void testMocksControl_Interface() { |
58 |
2
|
final IMocksControl ctrl = createControl(); |
59 |
2
|
final List<?> list = ctrl.createMock(List.class); |
60 |
2
|
testList(ctrl, list); |
61 |
|
} |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
1
PASS
|
|
63 |
2
|
@Test... |
64 |
|
public void testMocksControl_Class() { |
65 |
2
|
final IMocksControl ctrl = createControl(); |
66 |
2
|
final ArrayList<?> list = ctrl.createMock(ArrayList.class); |
67 |
2
|
testList(ctrl, list); |
68 |
|
} |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
1
PASS
|
|
70 |
2
|
@Test... |
71 |
|
public void testMocksControl_Class_WithName() { |
72 |
2
|
final IMocksControl ctrl = createControl(); |
73 |
2
|
final ArrayList<?> list = ctrl.createMock("myMock", ArrayList.class); |
74 |
2
|
testList(ctrl, list); |
75 |
|
} |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0,12 |
1
PASS
|
|
77 |
2
|
@SuppressWarnings("deprecation")... |
78 |
|
@Test |
79 |
|
public void testMocksControl_PartialMock_NoConstructorCalled() throws Exception { |
80 |
2
|
final IMocksControl ctrl = createControl(); |
81 |
2
|
final A a = ctrl.createMock(A.class, A.class.getMethod("bar", new Class[0]), A.class.getMethod( |
82 |
|
"toString", new Class[0])); |
83 |
|
|
84 |
2
|
assertEquals("No constructor called so should not be initialized", 0, a.i); |
85 |
2
|
expect(a.bar()).andReturn(5); |
86 |
2
|
replay(a); |
87 |
2
|
assertEquals("foo isn't mocked so it will call bar which return 5", 5, a.foo()); |
88 |
2
|
verify(a); |
89 |
|
|
90 |
2
|
assertEquals("EasyMock for class org.easymock.tests2.MocksControlTest$A", a.toString()); |
91 |
|
} |
92 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0,12 |
1
PASS
|
|
93 |
2
|
@SuppressWarnings("deprecation")... |
94 |
|
@Test |
95 |
|
public void testMocksControl_NamedPartialMock_NoConstructorCalled() throws Exception { |
96 |
2
|
final IMocksControl ctrl = createControl(); |
97 |
2
|
final A a = ctrl.createMock("myMock", A.class, A.class.getMethod("bar", new Class[0]), A.class |
98 |
|
.getMethod("toString", new Class[0])); |
99 |
|
|
100 |
2
|
assertEquals("No constructor called so should not be initialized", 0, a.i); |
101 |
2
|
expect(a.bar()).andReturn(5); |
102 |
2
|
replay(a); |
103 |
2
|
assertEquals("foo isn't mocked so it will call bar which return 5", 5, a.foo()); |
104 |
2
|
verify(a); |
105 |
|
|
106 |
2
|
assertEquals("myMock", a.toString()); |
107 |
|
} |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0,11 |
1
PASS
|
|
109 |
2
|
@SuppressWarnings("deprecation")... |
110 |
|
@Test |
111 |
|
public void testMocksControl_PartialMock_ConstructorCalled() throws Exception { |
112 |
2
|
final IMocksControl ctrl = createControl(); |
113 |
|
|
114 |
2
|
final ConstructorArgs args = new ConstructorArgs(A.class.getConstructor(Integer.TYPE), 6); |
115 |
|
|
116 |
2
|
final A a = ctrl.createMock(A.class, args, A.class.getMethod("bar", new Class[0]), A.class.getMethod( |
117 |
|
"toString", new Class[0])); |
118 |
|
|
119 |
2
|
assertEquals("Constructor called so should be initialized", 6, a.i); |
120 |
2
|
expect(a.bar()).andReturn(5); |
121 |
2
|
replay(a); |
122 |
2
|
assertEquals("foo isn't mocked so it will call bar which return 5", 5, a.foo()); |
123 |
2
|
verify(a); |
124 |
|
|
125 |
2
|
assertEquals("EasyMock for class org.easymock.tests2.MocksControlTest$A", a.toString()); |
126 |
|
} |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0,11 |
1
PASS
|
|
128 |
2
|
@SuppressWarnings("deprecation")... |
129 |
|
@Test |
130 |
|
public void testMocksControl_NamedPartialMock_ConstructorCalled() throws Exception { |
131 |
2
|
final IMocksControl ctrl = createControl(); |
132 |
|
|
133 |
2
|
final ConstructorArgs args = new ConstructorArgs(A.class.getConstructor(Integer.TYPE), 6); |
134 |
|
|
135 |
2
|
final A a = ctrl.createMock("myMock", A.class, args, A.class.getMethod("bar", new Class[0]), A.class |
136 |
|
.getMethod("toString", new Class[0])); |
137 |
|
|
138 |
2
|
assertEquals("Constructor called so should be initialized", 6, a.i); |
139 |
2
|
expect(a.bar()).andReturn(5); |
140 |
2
|
replay(a); |
141 |
2
|
assertEquals("foo isn't mocked so it will call bar which return 5", 5, a.foo()); |
142 |
2
|
verify(a); |
143 |
|
|
144 |
2
|
assertEquals("myMock", a.toString()); |
145 |
|
} |
146 |
|
|
|
|
| 73,3% |
Uncovered Elements: 4 (15) |
Complexity: 5 |
Complexity Density: 0,33 |
1
PASS
|
|
147 |
2
|
@SuppressWarnings("deprecation")... |
148 |
|
@Test |
149 |
|
public void testInterfaceForbidden_PartialMock() throws Exception { |
150 |
2
|
final ConstructorArgs args = new ConstructorArgs(ArrayList.class.getConstructor(Integer.TYPE), 6); |
151 |
2
|
final Method[] methods = new Method[] { List.class.getMethod("size", new Class[0]) }; |
152 |
|
|
153 |
2
|
final IMocksControl ctrl = createControl(); |
154 |
|
|
155 |
2
|
try { |
156 |
2
|
ctrl.createMock(List.class, methods); |
157 |
0
|
fail("partial mocking on interface shouln't be allowed"); |
158 |
|
} catch (final IllegalArgumentException e) { |
159 |
|
} |
160 |
|
|
161 |
2
|
try { |
162 |
2
|
ctrl.createMock(List.class, args, methods); |
163 |
0
|
fail("partial mocking on interface shouln't be allowed"); |
164 |
|
} catch (final IllegalArgumentException e) { |
165 |
|
} |
166 |
|
|
167 |
2
|
try { |
168 |
2
|
ctrl.createMock("myMock", List.class, methods); |
169 |
0
|
fail("partial mocking on interface shouln't be allowed"); |
170 |
|
} catch (final IllegalArgumentException e) { |
171 |
|
} |
172 |
|
|
173 |
2
|
try { |
174 |
2
|
ctrl.createMock("myMock", List.class, args, methods); |
175 |
0
|
fail("partial mocking on interface shouln't be allowed"); |
176 |
|
} catch (final IllegalArgumentException e) { |
177 |
|
} |
178 |
|
} |
179 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
180 |
6
|
private void testList(final IMocksControl ctrl, final List<?> list) {... |
181 |
6
|
expect(list.size()).andReturn(3); |
182 |
6
|
ctrl.replay(); |
183 |
6
|
assertEquals(3, list.size()); |
184 |
6
|
ctrl.verify(); |
185 |
|
} |
186 |
|
} |