| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
package org.easymock.tests2; |
| 6 |
|
|
| 7 |
|
import static org.easymock.EasyMock.*; |
| 8 |
|
import static org.junit.Assert.*; |
| 9 |
|
|
| 10 |
|
import org.easymock.internal.ReplayState; |
| 11 |
|
import org.easymock.tests.IMethods; |
| 12 |
|
import org.easymock.tests.Util; |
| 13 |
|
import org.junit.Before; |
| 14 |
|
import org.junit.Test; |
| 15 |
|
|
|
|
|
| 88% |
Uncovered Elements: 14 (117) |
Complexity: 22 |
Complexity Density: 0,23 |
|
| 16 |
|
public class UsageStrictMockTest { |
| 17 |
|
private IMethods mock; |
| 18 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
| 19 |
7
|
@Before... |
| 20 |
|
public void setup() { |
| 21 |
7
|
mock = createStrictMock(IMethods.class); |
| 22 |
7
|
mock.simpleMethodWithArgument("1"); |
| 23 |
7
|
mock.simpleMethodWithArgument("2"); |
| 24 |
7
|
replay(mock); |
| 25 |
|
} |
| 26 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
1
PASS
|
|
| 27 |
1
|
@Test... |
| 28 |
|
public void orderedCallsSucces() { |
| 29 |
1
|
mock.simpleMethodWithArgument("1"); |
| 30 |
1
|
mock.simpleMethodWithArgument("2"); |
| 31 |
1
|
verify(mock); |
| 32 |
|
} |
| 33 |
|
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0,5 |
1
PASS
|
|
| 34 |
1
|
@Test... |
| 35 |
|
public void unorderedCallsFailure() { |
| 36 |
1
|
boolean failed = false; |
| 37 |
1
|
try { |
| 38 |
1
|
mock.simpleMethodWithArgument("2"); |
| 39 |
|
} catch (AssertionError expected) { |
| 40 |
1
|
failed = true; |
| 41 |
|
} |
| 42 |
1
|
if (!failed) { |
| 43 |
0
|
fail("unordered calls accepted"); |
| 44 |
|
} |
| 45 |
|
} |
| 46 |
|
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0,38 |
1
PASS
|
|
| 47 |
1
|
@Test... |
| 48 |
|
public void tooManyCallsFailure() { |
| 49 |
1
|
mock.simpleMethodWithArgument("1"); |
| 50 |
1
|
mock.simpleMethodWithArgument("2"); |
| 51 |
|
|
| 52 |
1
|
boolean failed = false; |
| 53 |
1
|
try { |
| 54 |
1
|
mock.simpleMethodWithArgument("2"); |
| 55 |
|
} catch (AssertionError expected) { |
| 56 |
1
|
failed = true; |
| 57 |
|
} |
| 58 |
1
|
if (!failed) { |
| 59 |
0
|
fail("too many calls accepted"); |
| 60 |
|
} |
| 61 |
|
} |
| 62 |
|
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0,38 |
1
PASS
|
|
| 63 |
1
|
@Test... |
| 64 |
|
public void tooFewCallsFailure() { |
| 65 |
1
|
mock.simpleMethodWithArgument("1"); |
| 66 |
1
|
boolean failed = false; |
| 67 |
1
|
try { |
| 68 |
1
|
verify(mock); |
| 69 |
|
} catch (AssertionError expected) { |
| 70 |
1
|
failed = true; |
| 71 |
1
|
assertTrue("stack trace must be filled in", Util.getStackTrace( |
| 72 |
|
expected).indexOf(ReplayState.class.getName()) == -1); |
| 73 |
|
} |
| 74 |
1
|
if (!failed) { |
| 75 |
0
|
fail("too few calls accepted"); |
| 76 |
|
} |
| 77 |
|
} |
| 78 |
|
|
|
|
|
| 86,7% |
Uncovered Elements: 4 (30) |
Complexity: 5 |
Complexity Density: 0,19 |
1
PASS
|
|
| 79 |
1
|
@Test... |
| 80 |
|
public void differentMethods() { |
| 81 |
|
|
| 82 |
1
|
reset(mock); |
| 83 |
|
|
| 84 |
1
|
mock.booleanReturningMethod(0); |
| 85 |
1
|
expectLastCall().andReturn(true); |
| 86 |
1
|
mock.simpleMethod(); |
| 87 |
1
|
mock.booleanReturningMethod(1); |
| 88 |
1
|
expectLastCall().andReturn(false).times(2, 3); |
| 89 |
1
|
mock.simpleMethod(); |
| 90 |
1
|
expectLastCall().atLeastOnce(); |
| 91 |
|
|
| 92 |
1
|
replay(mock); |
| 93 |
1
|
assertEquals(true, mock.booleanReturningMethod(0)); |
| 94 |
1
|
mock.simpleMethod(); |
| 95 |
|
|
| 96 |
1
|
boolean failed = false; |
| 97 |
1
|
try { |
| 98 |
1
|
verify(mock); |
| 99 |
|
} catch (AssertionError expected) { |
| 100 |
1
|
failed = true; |
| 101 |
1
|
assertEquals( |
| 102 |
|
"\n Expectation failure on verify:" |
| 103 |
|
+ "\n simpleMethod(): expected: 1, actual: 1" |
| 104 |
|
+ "\n booleanReturningMethod(1): expected: between 2 and 3, actual: 0" |
| 105 |
|
+ "\n simpleMethod(): expected: at least 1, actual: 0", |
| 106 |
|
expected.getMessage()); |
| 107 |
|
} |
| 108 |
1
|
if (!failed) { |
| 109 |
0
|
fail("too few calls accepted"); |
| 110 |
|
} |
| 111 |
|
|
| 112 |
1
|
assertEquals(false, mock.booleanReturningMethod(1)); |
| 113 |
|
|
| 114 |
1
|
failed = false; |
| 115 |
1
|
try { |
| 116 |
1
|
mock.simpleMethod(); |
| 117 |
|
} catch (AssertionError expected) { |
| 118 |
1
|
failed = true; |
| 119 |
1
|
assertEquals( |
| 120 |
|
"\n Unexpected method call simpleMethod():" |
| 121 |
|
+ "\n booleanReturningMethod(1): expected: between 2 and 3, actual: 1", |
| 122 |
|
expected.getMessage()); |
| 123 |
|
} |
| 124 |
1
|
if (!failed) { |
| 125 |
0
|
fail("wrong call accepted"); |
| 126 |
|
} |
| 127 |
|
} |
| 128 |
|
|
|
|
|
| 91,7% |
Uncovered Elements: 2 (24) |
Complexity: 3 |
Complexity Density: 0,14 |
1
PASS
|
|
| 129 |
1
|
@Test... |
| 130 |
|
public void range() { |
| 131 |
|
|
| 132 |
1
|
reset(mock); |
| 133 |
|
|
| 134 |
1
|
mock.booleanReturningMethod(0); |
| 135 |
1
|
expectLastCall().andReturn(true); |
| 136 |
1
|
mock.simpleMethod(); |
| 137 |
1
|
mock.booleanReturningMethod(1); |
| 138 |
1
|
expectLastCall().andReturn(false).times(2, 3); |
| 139 |
1
|
mock.simpleMethod(); |
| 140 |
1
|
expectLastCall().atLeastOnce(); |
| 141 |
1
|
expect(mock.booleanReturningMethod(1)).andReturn(false); |
| 142 |
|
|
| 143 |
1
|
replay(mock); |
| 144 |
|
|
| 145 |
1
|
mock.booleanReturningMethod(0); |
| 146 |
1
|
mock.simpleMethod(); |
| 147 |
|
|
| 148 |
1
|
mock.booleanReturningMethod(1); |
| 149 |
1
|
mock.booleanReturningMethod(1); |
| 150 |
1
|
mock.booleanReturningMethod(1); |
| 151 |
|
|
| 152 |
1
|
boolean failed = false; |
| 153 |
|
|
| 154 |
1
|
try { |
| 155 |
1
|
mock.booleanReturningMethod(1); |
| 156 |
|
} catch (AssertionError expected) { |
| 157 |
1
|
failed = true; |
| 158 |
1
|
assertEquals( |
| 159 |
|
"\n Unexpected method call booleanReturningMethod(1):" |
| 160 |
|
+ "\n booleanReturningMethod(1): expected: between 2 and 3, actual: 3 (+1)" |
| 161 |
|
+ "\n simpleMethod(): expected: at least 1, actual: 0", |
| 162 |
|
expected.getMessage()); |
| 163 |
|
} |
| 164 |
1
|
if (!failed) { |
| 165 |
0
|
fail("too many calls accepted"); |
| 166 |
|
} |
| 167 |
|
} |
| 168 |
|
|
|
|
|
| 90% |
Uncovered Elements: 2 (20) |
Complexity: 3 |
Complexity Density: 0,17 |
1
PASS
|
|
| 169 |
1
|
@Test... |
| 170 |
|
public void stubBehavior() { |
| 171 |
1
|
reset(mock); |
| 172 |
|
|
| 173 |
1
|
mock.booleanReturningMethod(1); |
| 174 |
1
|
expectLastCall().andReturn(true).andReturn(false).andReturn(true); |
| 175 |
1
|
mock.booleanReturningMethod(anyInt()); |
| 176 |
1
|
expectLastCall().andStubReturn(true); |
| 177 |
|
|
| 178 |
1
|
replay(mock); |
| 179 |
|
|
| 180 |
1
|
assertEquals(true, mock.booleanReturningMethod(2)); |
| 181 |
1
|
assertEquals(true, mock.booleanReturningMethod(3)); |
| 182 |
1
|
assertEquals(true, mock.booleanReturningMethod(1)); |
| 183 |
1
|
assertEquals(false, mock.booleanReturningMethod(1)); |
| 184 |
1
|
assertEquals(true, mock.booleanReturningMethod(3)); |
| 185 |
|
|
| 186 |
1
|
boolean failed = false; |
| 187 |
1
|
try { |
| 188 |
1
|
verify(mock); |
| 189 |
|
} catch (AssertionError expected) { |
| 190 |
1
|
failed = true; |
| 191 |
1
|
assertEquals( |
| 192 |
|
"\n Expectation failure on verify:" |
| 193 |
|
+ "\n booleanReturningMethod(1): expected: 3, actual: 2", |
| 194 |
|
expected.getMessage()); |
| 195 |
|
} |
| 196 |
1
|
if (!failed) { |
| 197 |
0
|
fail("too few calls accepted"); |
| 198 |
|
} |
| 199 |
|
} |
| 200 |
|
} |