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.junit.Test; |
23 |
|
|
24 |
|
|
25 |
|
@author |
26 |
|
|
|
|
| 93,2% |
Uncovered Elements: 4 (59) |
Complexity: 13 |
Complexity Density: 0,26 |
|
27 |
|
public class DelegateToTest { |
28 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
29 |
|
public static interface IMyInterface { |
30 |
|
int getInt(int k); |
31 |
|
} |
32 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0,1 |
1
PASS
|
|
33 |
2
|
@Test... |
34 |
|
public void testDelegate() { |
35 |
2
|
final IMyInterface mock = createMock(IMyInterface.class); |
36 |
2
|
final IMyInterface delegateTo = new IMyInterface() { |
37 |
|
private int i = 0; |
38 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
39 |
8
|
public int getInt(final int k) {... |
40 |
8
|
return i += k; |
41 |
|
} |
42 |
|
}; |
43 |
|
|
44 |
2
|
expect(mock.getInt(10)).andDelegateTo(delegateTo); |
45 |
2
|
expect(mock.getInt(5)).andDelegateTo(delegateTo).andDelegateTo(delegateTo).times(2); |
46 |
|
|
47 |
2
|
replay(mock); |
48 |
|
|
49 |
2
|
assertEquals(10, mock.getInt(10)); |
50 |
2
|
assertEquals(15, mock.getInt(5)); |
51 |
2
|
assertEquals(20, mock.getInt(5)); |
52 |
2
|
assertEquals(25, mock.getInt(5)); |
53 |
|
|
54 |
2
|
verify(mock); |
55 |
|
} |
56 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0,09 |
1
PASS
|
|
57 |
2
|
@Test... |
58 |
|
public void testStubDelegate() { |
59 |
2
|
final IMyInterface mock = createMock(IMyInterface.class); |
60 |
2
|
final IMyInterface delegateTo = new IMyInterface() { |
61 |
|
private int i = 0; |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
63 |
8
|
public int getInt(final int k) {... |
64 |
8
|
return ++i; |
65 |
|
} |
66 |
|
}; |
67 |
2
|
expect(mock.getInt(5)).andReturn(3).andStubDelegateTo(delegateTo); |
68 |
2
|
expect(mock.getInt(20)).andStubDelegateTo(delegateTo); |
69 |
|
|
70 |
2
|
replay(mock); |
71 |
|
|
72 |
2
|
assertEquals(3, mock.getInt(5)); |
73 |
2
|
assertEquals(1, mock.getInt(5)); |
74 |
2
|
assertEquals(2, mock.getInt(5)); |
75 |
2
|
assertEquals(3, mock.getInt(20)); |
76 |
2
|
assertEquals(4, mock.getInt(20)); |
77 |
|
|
78 |
2
|
verify(mock); |
79 |
|
} |
80 |
|
|
|
|
| 88,9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0,22 |
1
PASS
|
|
81 |
2
|
@Test... |
82 |
|
public void testReturnException() { |
83 |
2
|
final IMyInterface m = createMock(IMyInterface.class); |
84 |
2
|
final IMyInterface delegateTo = new IMyInterface() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
85 |
2
|
public int getInt(final int k) {... |
86 |
2
|
throw new ArithmeticException("Not good!"); |
87 |
|
} |
88 |
|
}; |
89 |
2
|
expect(m.getInt(5)).andDelegateTo(delegateTo); |
90 |
|
|
91 |
2
|
replay(m); |
92 |
|
|
93 |
2
|
try { |
94 |
2
|
m.getInt(5); |
95 |
0
|
fail(); |
96 |
|
} catch (final ArithmeticException e) { |
97 |
2
|
assertEquals("Not good!", e.getMessage()); |
98 |
|
} |
99 |
|
|
100 |
2
|
verify(m); |
101 |
|
} |
102 |
|
|
|
|
| 85,7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0,29 |
1
PASS
|
|
103 |
2
|
@Test... |
104 |
|
public void testWrongClass() { |
105 |
2
|
final IMyInterface m = createMock(IMyInterface.class); |
106 |
2
|
expect(m.getInt(0)).andDelegateTo("allo"); |
107 |
2
|
replay(m); |
108 |
2
|
try { |
109 |
2
|
m.getInt(0); |
110 |
0
|
fail("Should throw an exception"); |
111 |
|
} catch (final IllegalArgumentException e) { |
112 |
2
|
assertEquals( |
113 |
|
"Delegation to object [allo] is not implementing the mocked method [public abstract int org.easymock.tests2.DelegateToTest$IMyInterface.getInt(int)]", |
114 |
|
e.getMessage()); |
115 |
|
} |
116 |
|
} |
117 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0,4 |
1
PASS
|
|
118 |
2
|
@Test... |
119 |
|
public void nullDelegationNotAllowed() { |
120 |
2
|
final IMyInterface mock = createMock(IMyInterface.class); |
121 |
2
|
try { |
122 |
2
|
expect(mock.getInt(1)).andDelegateTo(null); |
123 |
0
|
fail(); |
124 |
|
} catch (final NullPointerException expected) { |
125 |
2
|
assertEquals("delegated to object must not be null", expected.getMessage()); |
126 |
|
} |
127 |
|
} |
128 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0,4 |
1
PASS
|
|
129 |
2
|
@Test... |
130 |
|
public void nullStubDelegationNotAllowed() { |
131 |
2
|
final IMyInterface mock = createMock(IMyInterface.class); |
132 |
2
|
try { |
133 |
2
|
expect(mock.getInt(1)).andStubDelegateTo(null); |
134 |
0
|
fail(); |
135 |
|
} catch (final NullPointerException expected) { |
136 |
2
|
assertEquals("delegated to object must not be null", expected.getMessage()); |
137 |
|
} |
138 |
|
} |
139 |
|
} |