1
2
3
4
5
6 package mockit.internal.reflection;
7
8 import static org.junit.jupiter.api.Assertions.assertEquals;
9 import static org.junit.jupiter.api.Assertions.assertFalse;
10 import static org.junit.jupiter.api.Assertions.assertNotNull;
11 import static org.junit.jupiter.api.Assertions.assertNull;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
17
18 import org.junit.jupiter.api.Test;
19
20 final class MockInvocationHandlerTest {
21
22 interface SampleInterface {
23 String doSomething();
24
25 int getValue();
26 }
27
28 @Test
29 void instanceIsNotNull() {
30 assertNotNull(MockInvocationHandler.INSTANCE);
31 }
32
33 @Test
34 void newMockedInstanceCreatesProxyForInterface() {
35 SampleInterface proxy = (SampleInterface) Proxy.newProxyInstance(SampleInterface.class.getClassLoader(),
36 new Class<?>[] { SampleInterface.class }, MockInvocationHandler.INSTANCE);
37 Class<?> proxyClass = proxy.getClass();
38 Object instance = MockInvocationHandler.newMockedInstance(proxyClass);
39 assertNotNull(instance);
40 assertTrue(instance instanceof SampleInterface);
41 }
42
43 @Test
44 void invokeEqualsWithSelf() throws Throwable {
45 SampleInterface proxy = (SampleInterface) Proxy.newProxyInstance(SampleInterface.class.getClassLoader(),
46 new Class<?>[] { SampleInterface.class }, MockInvocationHandler.INSTANCE);
47
48 Method equalsMethod = Object.class.getMethod("equals", Object.class);
49 Object result = MockInvocationHandler.INSTANCE.invoke(proxy, equalsMethod, new Object[] { proxy });
50
51 assertTrue((Boolean) result);
52 }
53
54 @Test
55 void invokeEqualsWithDifferentObject() throws Throwable {
56 SampleInterface proxy = (SampleInterface) Proxy.newProxyInstance(SampleInterface.class.getClassLoader(),
57 new Class<?>[] { SampleInterface.class }, MockInvocationHandler.INSTANCE);
58
59 Method equalsMethod = Object.class.getMethod("equals", Object.class);
60 Object result = MockInvocationHandler.INSTANCE.invoke(proxy, equalsMethod, new Object[] { "different" });
61
62 assertFalse((Boolean) result);
63 }
64
65 @Test
66 void invokeHashCode() throws Throwable {
67 SampleInterface proxy = (SampleInterface) Proxy.newProxyInstance(SampleInterface.class.getClassLoader(),
68 new Class<?>[] { SampleInterface.class }, MockInvocationHandler.INSTANCE);
69
70 Method hashCodeMethod = Object.class.getMethod("hashCode");
71 Object result = MockInvocationHandler.INSTANCE.invoke(proxy, hashCodeMethod, null);
72
73 assertEquals(System.identityHashCode(proxy), result);
74 }
75
76 @Test
77 void invokeToString() throws Throwable {
78 SampleInterface proxy = (SampleInterface) Proxy.newProxyInstance(SampleInterface.class.getClassLoader(),
79 new Class<?>[] { SampleInterface.class }, MockInvocationHandler.INSTANCE);
80
81 Method toStringMethod = Object.class.getMethod("toString");
82 Object result = MockInvocationHandler.INSTANCE.invoke(proxy, toStringMethod, null);
83
84 assertNotNull(result);
85 assertTrue(result instanceof String);
86 }
87
88 @Test
89 void invokeInterfaceMethodReturnsDefaultValue() throws Throwable {
90 SampleInterface proxy = (SampleInterface) Proxy.newProxyInstance(SampleInterface.class.getClassLoader(),
91 new Class<?>[] { SampleInterface.class }, MockInvocationHandler.INSTANCE);
92
93 Method doSomethingMethod = SampleInterface.class.getMethod("doSomething");
94 Object result = MockInvocationHandler.INSTANCE.invoke(proxy, doSomethingMethod, null);
95
96 assertNull(result);
97 }
98
99 @Test
100 void invokeInterfaceMethodReturningIntReturnsZero() throws Throwable {
101 SampleInterface proxy = (SampleInterface) Proxy.newProxyInstance(SampleInterface.class.getClassLoader(),
102 new Class<?>[] { SampleInterface.class }, MockInvocationHandler.INSTANCE);
103
104 Method getValueMethod = SampleInterface.class.getMethod("getValue");
105 Object result = MockInvocationHandler.INSTANCE.invoke(proxy, getValueMethod, null);
106
107 assertEquals(0, result);
108 }
109
110 @Test
111 void invokeAnnotationTypeMethodReturnsAnnotationType() throws Throwable {
112
113 Annotation proxy = (Annotation) Proxy.newProxyInstance(Annotation.class.getClassLoader(),
114 new Class<?>[] { Deprecated.class }, MockInvocationHandler.INSTANCE);
115
116 Method annotationTypeMethod = Annotation.class.getMethod("annotationType");
117 Object result = MockInvocationHandler.INSTANCE.invoke(proxy, annotationTypeMethod, null);
118
119 assertNotNull(result);
120 assertEquals(Deprecated.class, result);
121 }
122 }