1
2
3
4
5
6 package mockit;
7
8 import static org.junit.jupiter.api.Assertions.assertEquals;
9
10 import java.io.Serializable;
11
12 import mockit.MockingMultipleInterfacesTest.Dependency;
13 import mockit.integration.junit5.JMockitExtension;
14
15 import org.junit.jupiter.api.Test;
16 import org.junit.jupiter.api.extension.ExtendWith;
17
18
19
20
21
22
23
24 @ExtendWith(JMockitExtension.class)
25 class MockingMultipleInterfacesTest<MultiMock extends Dependency & Runnable> {
26
27
28
29
30 interface Dependency {
31
32
33
34
35
36
37
38
39 String doSomething(boolean b);
40 }
41
42
43 @Mocked
44 MultiMock multiMock;
45
46
47
48
49 @Test
50 void mockFieldWithTwoInterfaces() {
51 new Expectations() {
52 {
53 multiMock.doSomething(false);
54 result = "test";
55 }
56 };
57
58 multiMock.run();
59 assertEquals("test", multiMock.doSomething(false));
60
61 new Verifications() {
62 {
63 multiMock.run();
64 }
65 };
66 }
67
68
69
70
71
72
73
74
75
76 @Test
77 <M extends Dependency & Serializable> void mockParameterWithTwoInterfaces(@Mocked final M mock) {
78 new Expectations() {
79 {
80 mock.doSomething(true);
81 result = "test";
82 }
83 };
84
85 assertEquals("test", mock.doSomething(true));
86 }
87
88
89
90
91 public interface Base {
92
93
94
95 void doSomething();
96 }
97
98
99
100
101 abstract static class Derived implements Base {
102
103
104
105 protected Derived() {
106 }
107 }
108
109
110
111
112 public abstract static class ToBeMocked extends Derived {
113 }
114
115
116
117
118
119
120
121 @Test
122 void mockAbstractMethodInheritedFromInterfaceImplementedBySuperClass(@Mocked final ToBeMocked mock) {
123 mock.doSomething();
124
125 new Verifications() {
126 {
127 mock.doSomething();
128 times = 1;
129 }
130 };
131 }
132 }