-
Notifications
You must be signed in to change notification settings - Fork 237
/
FakingBaseTypesTest.java
241 lines (187 loc) · 7.24 KB
/
FakingBaseTypesTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package mockit;
import java.io.*;
import java.nio.*;
import javax.annotation.*;
import org.junit.*;
import org.junit.runners.*;
import static org.junit.Assert.*;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public final class FakingBaseTypesTest
{
static class BeforeClassBaseAction { protected int perform() { return -1; } }
static class BeforeClassAction extends BeforeClassBaseAction { @Override protected int perform() { return 12; } }
@BeforeClass
public static <T extends BeforeClassBaseAction> void applyFakeForAllTests() {
new MockUp<T>() {
@Mock int perform() { return 34; }
};
}
@AfterClass
public static void verifyFakeForAllTestsIsInEffect() {
int i1 = new BeforeClassAction().perform();
int i2 = new BeforeClassBaseAction().perform();
assertEquals(34, i1);
assertEquals(34, i2);
}
public interface IBeforeAction { @SuppressWarnings("unused") int perform(); }
static class BeforeAction implements IBeforeAction { @Override public int perform() { return 123; } }
@Before
public <T extends IBeforeAction> void applyFakeForEachTest() {
new MockUp<T>() {
@Mock int perform() { return 56; }
};
}
@After
public void verifyFakeForEachTestIsInEffect() {
int i = new BeforeAction().perform();
assertEquals(56, i);
}
public interface IAction {
int perform(int i);
boolean notToBeFaked();
}
public static class ActionImpl1 implements IAction {
@Override public int perform(int i) { return i - 1; }
@Override public boolean notToBeFaked() { return true; }
}
static final class ActionImpl2 implements IAction {
@Override public int perform(int i) { return i - 2; }
@Override public boolean notToBeFaked() { return true; }
}
IAction actionI;
@Test
public <T extends IAction> void test3_fakeInterfaceImplementationClassesUsingAnonymousFake() {
actionI = new ActionImpl1();
new MockUp<T>() {
@Mock
int perform(int i) { return i + 1; }
};
assertEquals(2, actionI.perform(1));
assertTrue(actionI.notToBeFaked());
ActionImpl2 impl2 = new ActionImpl2();
assertEquals(3, impl2.perform(2));
assertTrue(impl2.notToBeFaked());
}
public interface TestInterface { String getData(); }
public static final class FakeTestInterface<T extends TestInterface> extends MockUp<T> {
@Mock
public String getData(Invocation inv) { return "faked" + inv.proceed(); }
}
@Test
public void fakeAllClassesImplementingAnInterfaceUsingNamedFakeWithInvocationParameter() {
TestInterface impl1 = new TestInterface() { @Override public String getData() { return "1"; } };
TestInterface impl2 = new TestInterface() { @Override public String getData() { return "2"; } };
new FakeTestInterface();
String faked1 = impl1.getData();
String faked2 = impl2.getData();
assertEquals("faked1", faked1);
assertEquals("faked2", faked2);
}
public abstract static class BaseAction {
protected abstract int perform(int i);
public int toBeFakedAsWell() { return -1; }
int notToBeFaked() { return 1; }
}
static final class ConcreteAction1 extends BaseAction {
@Override public int perform(int i) { return i - 1; }
}
static class ConcreteAction2 extends BaseAction {
@Override protected int perform(int i) { return i - 2; }
@Override public int toBeFakedAsWell() { return super.toBeFakedAsWell() - 1; }
@Override int notToBeFaked() { return super.notToBeFaked() + 1; }
}
static class ConcreteAction3 extends ConcreteAction2 {
@Override public int perform(int i) { return i - 3; }
@Override public int toBeFakedAsWell() { return -3; }
@Override final int notToBeFaked() { return 3; }
}
BaseAction actionB;
@Test
public <T extends BaseAction> void test4_fakeConcreteSubclassesUsingAnonymousFake() {
actionB = new ConcreteAction1();
new MockUp<T>() {
@Mock int perform(int i) { return i + 1; }
@Mock int toBeFakedAsWell() { return 123; }
};
assertEquals(2, actionB.perform(1));
assertEquals(123, actionB.toBeFakedAsWell());
assertEquals(1, actionB.notToBeFaked());
ConcreteAction2 action2 = new ConcreteAction2();
assertEquals(3, action2.perform(2));
assertEquals(123, action2.toBeFakedAsWell());
assertEquals(2, action2.notToBeFaked());
ConcreteAction3 action3 = new ConcreteAction3();
assertEquals(4, action3.perform(3));
assertEquals(123, action3.toBeFakedAsWell());
assertEquals(3, action3.notToBeFaked());
}
@After
public void checkImplementationClassesAreNoLongerFaked() {
if (actionI != null) {
assertEquals(-1, actionI.perform(0));
}
assertEquals(-2, new ActionImpl2().perform(0));
if (actionB != null) {
assertEquals(-1, actionB.perform(0));
}
assertEquals(-2, new ConcreteAction2().perform(0));
assertEquals(-3, new ConcreteAction3().perform(0));
}
static final class FakeInterface<T extends IAction> extends MockUp<T> {
@Mock
int perform(int i) { return i + 2; }
}
@Test
public void test5_fakeInterfaceImplementationClassesUsingNamedFake() {
new FakeInterface();
actionI = new ActionImpl1();
assertEquals(3, actionI.perform(1));
assertEquals(4, new ActionImpl2().perform(2));
}
static final class FakeBaseClass<T extends BaseAction> extends MockUp<T> {
@Mock
int perform(int i) { return i + 3; }
}
@Test
public void test6_fakeConcreteSubclassesUsingNamedFake() {
new FakeBaseClass();
actionB = new ConcreteAction1();
assertEquals(4, actionB.perform(1));
assertEquals(5, new ConcreteAction2().perform(2));
assertEquals(6, new ConcreteAction3().perform(3));
}
interface GenericIAction<N extends Number> { N perform(N n); }
@Test
public <M extends GenericIAction<Number>> void test7_fakeImplementationsOfGenericInterface() {
GenericIAction<Number> actionNumber = new GenericIAction<Number>() {
@Override public Number perform(Number n) { return n.intValue() + 1; }
};
GenericIAction<Integer> actionInt = new GenericIAction<Integer>() {
@Override public Integer perform(Integer n) { return n + 1; }
};
GenericIAction<Long> actionL = new GenericIAction<Long>() {
@Override public Long perform(Long n) { return n + 2; }
};
new MockUp<M>() {
@Mock Number perform(Number n) { return n.intValue() - 1; }
};
Number n = actionNumber.perform(1);
assertEquals(0, n); // mocked
int i = actionInt.perform(2);
assertEquals(3, i); // not mocked
long l = actionL.perform(3L);
assertEquals(5, l); // not mocked
}
@Test
public <R extends Readable> void test8_excludeJREClassesFromFakingForSafety() throws Exception {
new MockUp<R>() {
@Mock
int read(CharBuffer cb) { return 123; }
};
CharBuffer buf = CharBuffer.allocate(10);
int r1 = new Readable() { @Override public int read(@Nonnull CharBuffer cb) { return 1; } }.read(buf);
assertEquals(123, r1);
int r2 = new StringReader("test").read(buf);
assertEquals(4, r2);
}
}