1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.easymock.tests; |
18 |
|
|
19 |
|
import static org.easymock.EasyMock.*; |
20 |
|
import static org.junit.Assert.*; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
|
24 |
|
import org.junit.Before; |
25 |
|
import org.junit.Test; |
26 |
|
|
27 |
|
|
28 |
|
@author |
29 |
|
|
|
|
| 93% |
Uncovered Elements: 4 (57) |
Complexity: 15 |
Complexity Density: 0,33 |
|
30 |
|
public class UsageThrowableTest { |
31 |
|
|
32 |
|
private IMethods mock; |
33 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
34 |
16
|
@Before... |
35 |
|
public void setup() { |
36 |
16
|
mock = createMock(IMethods.class); |
37 |
|
} |
38 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0,1 |
1
PASS
|
|
39 |
2
|
@Test... |
40 |
|
public void noUpperLimit() { |
41 |
2
|
mock.simpleMethodWithArgument("1"); |
42 |
2
|
expectLastCall().atLeastOnce(); |
43 |
2
|
mock.simpleMethodWithArgument("2"); |
44 |
2
|
replay(mock); |
45 |
2
|
mock.simpleMethodWithArgument("1"); |
46 |
2
|
mock.simpleMethodWithArgument("1"); |
47 |
2
|
mock.simpleMethodWithArgument("2"); |
48 |
2
|
mock.simpleMethodWithArgument("1"); |
49 |
2
|
mock.simpleMethodWithArgument("1"); |
50 |
2
|
verify(mock); |
51 |
|
} |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
53 |
2
|
@Test... |
54 |
|
public void throwRuntimeException() { |
55 |
2
|
testThrowUncheckedException(new RuntimeException()); |
56 |
|
} |
57 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
58 |
2
|
@Test... |
59 |
|
public void throwSubclassOfRuntimeException() { |
60 |
2
|
testThrowUncheckedException(new RuntimeException() { |
61 |
|
private static final long serialVersionUID = 1L; |
62 |
|
}); |
63 |
|
} |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
65 |
2
|
@Test... |
66 |
|
public void throwError() { |
67 |
2
|
testThrowUncheckedException(new Error()); |
68 |
|
} |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
70 |
2
|
@Test... |
71 |
|
public void throwSubclassOfError() { |
72 |
2
|
testThrowUncheckedException(new Error() { |
73 |
|
private static final long serialVersionUID = 1L; |
74 |
|
}); |
75 |
|
} |
76 |
|
|
|
|
| 87,5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0,25 |
|
77 |
8
|
private void testThrowUncheckedException(final Throwable throwable) {... |
78 |
8
|
expect(mock.throwsNothing(true)).andReturn("true"); |
79 |
8
|
expect(mock.throwsNothing(false)).andThrow(throwable); |
80 |
|
|
81 |
8
|
replay(mock); |
82 |
|
|
83 |
8
|
try { |
84 |
8
|
mock.throwsNothing(false); |
85 |
0
|
fail("Trowable expected"); |
86 |
|
} catch (final Throwable expected) { |
87 |
8
|
assertSame(throwable, expected); |
88 |
|
} |
89 |
|
|
90 |
8
|
assertEquals("true", mock.throwsNothing(true)); |
91 |
|
} |
92 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
93 |
2
|
@Test... |
94 |
|
public void throwCheckedException() throws IOException { |
95 |
2
|
testThrowCheckedException(new IOException()); |
96 |
|
} |
97 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
98 |
2
|
@Test... |
99 |
|
public void throwSubclassOfCheckedException() throws IOException { |
100 |
2
|
testThrowCheckedException(new IOException() { |
101 |
|
private static final long serialVersionUID = 1L; |
102 |
|
}); |
103 |
|
} |
104 |
|
|
|
|
| 83,3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0,25 |
|
105 |
4
|
private void testThrowCheckedException(final IOException expected) throws IOException {... |
106 |
4
|
try { |
107 |
4
|
expect(mock.throwsIOException(0)).andReturn("Value 0"); |
108 |
4
|
expect(mock.throwsIOException(1)).andThrow(expected); |
109 |
4
|
expect(mock.throwsIOException(2)).andReturn("Value 2"); |
110 |
|
} catch (final IOException e) { |
111 |
0
|
fail("Unexpected Exception"); |
112 |
|
} |
113 |
|
|
114 |
4
|
replay(mock); |
115 |
|
|
116 |
4
|
assertEquals("Value 0", mock.throwsIOException(0)); |
117 |
4
|
assertEquals("Value 2", mock.throwsIOException(2)); |
118 |
|
|
119 |
4
|
try { |
120 |
4
|
mock.throwsIOException(1); |
121 |
0
|
fail("IOException expected"); |
122 |
|
} catch (final IOException expectedException) { |
123 |
4
|
assertSame(expectedException, expected); |
124 |
|
} |
125 |
|
} |
126 |
|
|
|
|
| 88,9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0,22 |
1
PASS
|
|
127 |
2
|
@Test... |
128 |
|
public void throwAfterReturnValue() { |
129 |
2
|
final RuntimeException expectedException = new RuntimeException(); |
130 |
2
|
expect(mock.throwsNothing(false)).andReturn("").andThrow(expectedException); |
131 |
|
|
132 |
2
|
replay(mock); |
133 |
|
|
134 |
2
|
assertEquals("", mock.throwsNothing(false)); |
135 |
|
|
136 |
2
|
try { |
137 |
2
|
mock.throwsNothing(false); |
138 |
0
|
fail("RuntimeException expected"); |
139 |
|
} catch (final RuntimeException actualException) { |
140 |
2
|
assertSame(expectedException, actualException); |
141 |
|
} |
142 |
|
|
143 |
2
|
verify(mock); |
144 |
|
} |
145 |
|
|
146 |
|
} |