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