/
ProxyMockMakerTest.java
101 lines (81 loc) · 3.57 KB
/
ProxyMockMakerTest.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
/*
* Copyright (c) 2021 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.proxy;
import org.junit.Test;
import org.mockito.internal.creation.AbstractMockMakerTest;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import static org.assertj.core.api.Assertions.assertThat;
public class ProxyMockMakerTest
extends AbstractMockMakerTest<ProxyMockMaker, ProxyMockMakerTest.SomeInterface> {
public ProxyMockMakerTest() {
super(new ProxyMockMaker(), SomeInterface.class);
}
@Test
public void should_create_mock_from_interface() {
SomeInterface proxy =
mockMaker.createMock(settingsFor(SomeInterface.class), dummyHandler());
Class<?> superClass = proxy.getClass().getSuperclass();
assertThat(superClass).isEqualTo(Proxy.class);
}
@Test
public void should_create_mock_from_interface_with_extra_interface() {
SomeInterface proxy =
mockMaker.createMock(
settingsFor(SomeInterface.class, Serializable.class), dummyHandler());
Class<?> superClass = proxy.getClass().getSuperclass();
assertThat(superClass).isEqualTo(Proxy.class);
assertThat(proxy).isInstanceOf(Serializable.class);
}
@Test
public void should_discover_mockable_input() {
assertThat(mockMaker.isTypeMockable(Object.class).mockable()).isFalse();
assertThat(mockMaker.isTypeMockable(Object.class).nonMockableReason())
.isEqualTo("non-interface");
assertThat(mockMaker.isTypeMockable(SomeInterface.class).mockable()).isTrue();
}
@Test
public void can_compute_hash_code() throws Throwable {
SomeInterface proxy =
mockMaker.createMock(settingsFor(SomeInterface.class), dummyHandler());
InvocationHandler handler = Proxy.getInvocationHandler(proxy);
assertThat(handler.invoke(proxy, Object.class.getMethod("hashCode"), new Object[0]))
.isEqualTo(System.identityHashCode(proxy));
}
@Test
public void can_compute_equality() throws Throwable {
SomeInterface proxy =
mockMaker.createMock(settingsFor(SomeInterface.class), dummyHandler());
InvocationHandler handler = Proxy.getInvocationHandler(proxy);
assertThat(
handler.invoke(
proxy,
Object.class.getMethod("equals", Object.class),
new Object[] {proxy}))
.isEqualTo(true);
assertThat(
handler.invoke(
proxy,
Object.class.getMethod("equals", Object.class),
new Object[] {null}))
.isEqualTo(false);
assertThat(
handler.invoke(
proxy,
Object.class.getMethod("equals", Object.class),
new Object[] {new Object()}))
.isEqualTo(false);
}
@Test
public void can_invoke_toString() throws Throwable {
SomeInterface proxy =
mockMaker.createMock(settingsFor(SomeInterface.class), dummyHandler());
InvocationHandler handler = Proxy.getInvocationHandler(proxy);
assertThat(handler.invoke(proxy, Object.class.getMethod("toString"), new Object[0]))
.isNull();
}
interface SomeInterface {}
}