1
2
3
4
5
6 package mockit;
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.assertNull;
11 import static org.junit.jupiter.api.Assertions.assertTrue;
12 import static org.junit.jupiter.api.Assertions.fail;
13
14 import java.util.List;
15
16 import mockit.integration.junit5.JMockitExtension;
17
18 import org.junit.jupiter.api.BeforeEach;
19 import org.junit.jupiter.api.Test;
20 import org.junit.jupiter.api.extension.ExtendWith;
21
22
23
24
25 @ExtendWith(JMockitExtension.class)
26 class InjectableFieldTest {
27
28
29
30
31 static class Base {
32
33
34
35
36
37 protected int getValue() {
38 return 1;
39 }
40 }
41
42
43
44
45 static class Foo extends Base {
46
47
48
49
50
51
52
53 void doSomething(String s) {
54 throw new RuntimeException(s);
55 }
56
57
58
59
60
61
62 int getAnotherValue() {
63 return 2;
64 }
65
66
67
68
69
70
71 Boolean getBooleanValue() {
72 return true;
73 }
74
75
76
77
78
79
80 final List<Integer> getList() {
81 return null;
82 }
83
84
85
86
87
88
89 static String doSomethingElse() {
90 return "";
91 }
92 }
93
94
95 @Injectable
96 Foo foo;
97
98
99
100
101 @BeforeEach
102 void recordCommonExpectations() {
103 new Expectations() {
104 {
105 foo.getValue();
106 result = 12;
107 foo.getAnotherValue();
108 result = 123;
109 }
110 };
111
112 assertEquals(123, foo.getAnotherValue());
113 assertEquals(12, foo.getValue());
114 assertEquals(1, new Base().getValue());
115 assertEquals(2, new Foo().getAnotherValue());
116 }
117
118
119
120
121 @Test
122 void cascadeOneLevel() {
123 try {
124 new Foo().doSomething("");
125 fail();
126 } catch (RuntimeException ignore) {
127 }
128
129 new Expectations() {
130 {
131 foo.doSomething("test");
132 times = 1;
133 }
134 };
135
136 assertEquals(123, foo.getAnotherValue());
137 assertFalse(foo.getBooleanValue());
138 assertTrue(foo.getList().isEmpty());
139
140 foo.doSomething("test");
141 }
142
143
144
145
146 @Test
147 void overrideExpectationRecordedInBeforeMethod() {
148 new Expectations() {
149 {
150 foo.getAnotherValue();
151 result = 45;
152 }
153 };
154
155 assertEquals(45, foo.getAnotherValue());
156 foo.doSomething("sdf");
157 }
158
159
160
161
162 @Test
163 void partiallyMockInstanceWithoutAffectingInjectableInstances() {
164 final Foo localFoo = new Foo();
165
166 new Expectations(localFoo) {
167 {
168 localFoo.getAnotherValue();
169 result = 3;
170 Foo.doSomethingElse();
171 result = "test";
172 }
173 };
174
175 assertEquals(3, localFoo.getAnotherValue());
176 assertEquals(123, foo.getAnotherValue());
177 assertEquals(2, new Foo().getAnotherValue());
178 assertEquals("test", Foo.doSomethingElse());
179 foo.doSomething("");
180 }
181
182
183 @Injectable
184 int primitiveInt = 123;
185
186
187 @Injectable
188 Integer wrapperInt = 45;
189
190
191 @Injectable
192 String string = "Abc";
193
194
195
196
197 @Test
198 void useNonMockableInjectablesWithValuesProvidedThroughFieldAssignment() {
199 assertEquals(123, primitiveInt);
200 assertEquals(45, wrapperInt.intValue());
201 assertEquals("Abc", string);
202 }
203
204
205 @Injectable
206 int defaultInt;
207
208
209 @Injectable
210 Integer nullInteger;
211
212
213 @Injectable
214 String nullString;
215
216
217 @Injectable
218 String emptyString = "";
219
220
221
222
223 @Test
224 void useNullAndEmptyInjectablesOfNonMockableTypes() {
225 assertEquals(0, defaultInt);
226 assertNull(nullInteger);
227 assertNull(nullString);
228 assertTrue(emptyString.isEmpty());
229 }
230 }