forked from ahornace/mymockito
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyMockito.java
More file actions
65 lines (44 loc) · 1.48 KB
/
MyMockito.java
File metadata and controls
65 lines (44 loc) · 1.48 KB
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
package cz.cuni.mff.mymockito;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class MyMockito {
static class Triple {
Object o;
Method m;
Object returnValue;
}
static Triple[] triples = new Triple[10];
static Method lastMethod;
static Object lastObject;
public static <T> MyWhen<T> when(T t) {
return new MyWhen<>(lastObject, lastMethod);
}
static class MyWhen<T> {
public MyWhen(Object o, Method m) {
}
public void thenReturn(T t) {
triples[0] = new Triple();
}
}
static < T > T mock(Class<T> classObject){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(classObject); // parameter of mock method
enhancer.setCallback(new MyMockClassCallback());
return (T) enhancer.create(); // T is template arg of mock method
}
private static class MyMockClassCallback implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable{
lastMethod = method;
lastObject = o;
for (Triple t : triples) {
if (t.o == o && t.m == method) {
return t.returnValue;
}
}
return methodProxy.invokeSuper(o, args);
}
}
}