1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertNotNull;
5
6 import mockit.internal.expectations.invocation.MissingInvocation;
7
8 import org.junit.Test;
9
10
11
12
13 public final class ExpectationsForConstructorsTest {
14
15
16
17
18 public static class BaseCollaborator {
19
20
21 protected int value;
22
23
24
25
26 protected BaseCollaborator() {
27 value = -1;
28 }
29
30
31
32
33
34
35
36 protected BaseCollaborator(int value) {
37 this.value = value;
38 }
39 }
40
41
42
43
44 static class Collaborator extends BaseCollaborator {
45
46
47
48
49 Collaborator() {
50 }
51
52
53
54
55
56
57
58 Collaborator(int value) {
59 super(value);
60 }
61 }
62
63
64
65
66
67
68
69 @Test
70 public void mockAllConstructors(@Mocked Collaborator unused) {
71 new Expectations() {
72 {
73 new Collaborator();
74 new Collaborator(123);
75 }
76 };
77
78 assertEquals(0, new Collaborator().value);
79 assertEquals(0, new Collaborator(123).value);
80 }
81
82
83
84
85 static class A {
86
87
88
89
90 @SuppressWarnings("UnusedDeclaration")
91 private A() {
92 }
93
94
95
96
97
98
99
100 A(String s) {
101 assertNotNull("A(String) executed with null", s);
102 }
103 }
104
105
106
107
108 static class B extends A {
109
110
111
112
113
114
115 B(String s) {
116 super(s);
117 }
118 }
119
120
121
122
123
124
125
126 @Test
127 public void mockClassHierarchyWhereFirstConstructorInBaseClassIsPrivate(@Mocked B mock) {
128 new B("Test1");
129 }
130
131
132
133
134 static class D {
135
136
137
138
139
140
141 D(@SuppressWarnings("unused") String s) {
142 }
143 }
144
145
146
147
148
149
150
151 @Test
152 public void mockClassHierarchyWhereFirstConstructorInBaseClassOnAnotherPackageIsPackagePrivate(@Mocked D mock) {
153 assertNotNull(mock);
154 new D("Test1");
155 }
156
157
158
159
160 static class Base {
161 }
162
163
164
165
166 static class Derived extends Base {
167 }
168
169
170
171
172
173
174
175 @Test
176 public void recordAndReplayBaseConstructorInvocation(@Mocked Base mocked) {
177 new Expectations() {
178 {
179 new Base();
180 }
181 };
182
183 new Base();
184 }
185
186
187
188
189
190
191
192 @Test(expected = MissingInvocation.class)
193 public void recordExpectationOnBaseConstructorAndReplayWithCallToSuper(@Mocked Base mocked) {
194 new Expectations() {
195 {
196 new Base();
197 times = 1;
198 }
199 };
200
201 new Derived();
202 }
203
204
205
206
207
208
209
210 @Test(expected = MissingInvocation.class)
211 public void verifyExpectationOnBaseConstructorReplayedWithCallToSuper(@Mocked Base mocked) {
212 new Derived();
213
214 new Verifications() {
215 {
216 new Base();
217 }
218 };
219 }
220
221
222
223
224 static class Collaborator2 {
225
226
227
228
229
230
231
232 Collaborator2(@SuppressWarnings("unused") long l) {
233 }
234
235
236
237
238
239
240
241 Collaborator2(@SuppressWarnings("unused") Collaborator2 c) {
242 }
243
244
245
246
247 Collaborator2() {
248 this(new Collaborator2(123L));
249 }
250 }
251
252
253
254
255
256
257
258 @Test
259 public void mockConstructorWhichCallsTwoOthersOfTheSameClass(@Mocked Collaborator2 mock) {
260 new Collaborator2();
261 }
262 }