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.Constructor; |
23 |
|
import java.util.ArrayList; |
24 |
|
|
25 |
|
import org.easymock.ConstructorArgs; |
26 |
|
import org.junit.Test; |
27 |
|
|
28 |
|
|
29 |
|
@author |
30 |
|
|
|
|
| 82,1% |
Uncovered Elements: 5 (28) |
Complexity: 7 |
Complexity Density: 0,3 |
|
31 |
|
public class PartialMockingTest { |
32 |
|
|
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
33 |
|
public static abstract class A { |
34 |
|
|
35 |
|
public String s; |
36 |
|
|
37 |
|
public int i; |
38 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
39 |
2
|
protected A(final String s) {... |
40 |
2
|
this.s = s; |
41 |
|
} |
42 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
43 |
0
|
private A(final int i) {... |
44 |
0
|
this.i = i; |
45 |
|
} |
46 |
|
|
47 |
|
protected abstract int foo(); |
48 |
|
} |
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
1
PASS
|
|
50 |
2
|
@SuppressWarnings("unchecked")... |
51 |
|
@Test |
52 |
|
public void testPartialMock_PublicConstructor() throws Exception { |
53 |
2
|
final ArrayList<String> list = createMockBuilder(ArrayList.class).withConstructor(3).createMock(); |
54 |
2
|
list.add("test"); |
55 |
|
} |
56 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0,17 |
1
PASS
|
|
57 |
2
|
@Test... |
58 |
|
public void testPartialMock_ProtectedConstructor() throws Exception { |
59 |
2
|
final A a = createMockBuilder(A.class).withConstructor("test").createMock(); |
60 |
2
|
assertEquals("test", a.s); |
61 |
|
|
62 |
|
|
63 |
2
|
expect(a.foo()).andReturn(3); |
64 |
2
|
replay(a); |
65 |
2
|
assertEquals(3, a.foo()); |
66 |
2
|
verify(a); |
67 |
|
} |
68 |
|
|
|
|
| 33,3% |
Uncovered Elements: 4 (6) |
Complexity: 2 |
Complexity Density: 0,33 |
1
PASS
|
|
69 |
2
|
@Test(expected = RuntimeException.class)... |
70 |
|
public void testPartialMock_ConstructorNotFound() throws Exception { |
71 |
2
|
final Constructor<?> cstr = ArrayList.class.getConstructor(Integer.TYPE); |
72 |
2
|
final ConstructorArgs constructorArgs = new ConstructorArgs(cstr, 2.0); |
73 |
0
|
try { |
74 |
0
|
createMockBuilder(ArrayList.class).withConstructor(Integer.TYPE).withArgs(2.0).createMock(); |
75 |
|
} catch (final RuntimeException e) { |
76 |
0
|
assertEquals("Failed to find constructor for param types", e.getMessage()); |
77 |
0
|
throw e; |
78 |
|
} |
79 |
|
} |
80 |
|
|
|
|
| 66,7% |
Uncovered Elements: 1 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
1
PASS
|
|
81 |
2
|
@Test(expected = IllegalArgumentException.class)... |
82 |
|
public void testPartialMock_InvalidParams() throws Exception { |
83 |
2
|
final Constructor<?> cstr = ArrayList.class.getConstructor(Integer.TYPE); |
84 |
2
|
final ConstructorArgs constructorArgs = new ConstructorArgs(cstr, "test"); |
85 |
0
|
createMockBuilder(ArrayList.class).withConstructor(Integer.TYPE).withArgs("test"); |
86 |
|
} |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0,33 |
1
PASS
|
|
88 |
2
|
@Test(expected = RuntimeException.class)... |
89 |
|
public void testPartialMock_ExceptionInConstructor() throws Exception { |
90 |
2
|
final Constructor<?> cstr = ArrayList.class.getConstructor(Integer.TYPE); |
91 |
2
|
final ConstructorArgs constructorArgs = new ConstructorArgs(cstr, -5); |
92 |
2
|
try { |
93 |
2
|
createMockBuilder(ArrayList.class).withConstructor(-5).createMock(); |
94 |
|
} catch (final RuntimeException e) { |
95 |
2
|
assertEquals("Failed to instantiate mock calling constructor: Exception in constructor", e |
96 |
|
.getMessage()); |
97 |
2
|
throw e; |
98 |
|
} |
99 |
|
} |
100 |
|
} |