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.assertNotNull;
10 import static org.junit.jupiter.api.Assertions.assertNull;
11 import static org.junit.jupiter.api.Assertions.assertSame;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import mockit.integration.junit5.JMockitExtension;
15
16 import org.junit.jupiter.api.AfterEach;
17 import org.junit.jupiter.api.BeforeEach;
18 import org.junit.jupiter.api.Test;
19 import org.junit.jupiter.api.extension.ExtendWith;
20
21
22
23
24 @ExtendWith(JMockitExtension.class)
25 class TestedClassWithFieldDITest {
26
27
28 @Tested
29 TestedClassInGlobalScopeTest.TestedClass testedGlobal;
30
31
32
33
34 @Test
35 void useLocalTestedFieldHavingSameTypeAndNameAsGlobalTestedFieldInPreviousTestClass() {
36 assertNull(testedGlobal.someValue);
37 }
38
39
40
41
42 static final class UtilityClass {
43
44
45 String name;
46
47
48 int id;
49
50
51 Runnable action;
52
53
54 Collaborator collaborator;
55 }
56
57
58
59
60 public static class TestedClass {
61
62
63 static Runnable globalAction;
64
65
66 protected final int i;
67
68
69 protected Dependency dependency;
70
71
72
73
74 public TestedClass() {
75 i = -1;
76 }
77
78
79
80
81
82
83
84 public TestedClass(int i) {
85 this.i = i;
86 }
87
88
89
90
91
92
93 public boolean doSomeOperation() {
94 return dependency.doSomething() > 0;
95 }
96 }
97
98
99
100
101 static class Dependency {
102
103
104
105
106
107 int doSomething() {
108 return -1;
109 }
110 }
111
112
113
114
115 public static class Collaborator {
116 }
117
118
119 @Tested(availableDuringSetup = true)
120 UtilityClass util;
121
122
123 @Tested(availableDuringSetup = true, fullyInitialized = true)
124 UtilityClass util2;
125
126
127 @Injectable("util")
128 String utilName;
129
130
131 @Tested
132 TestedClass tested;
133
134
135 @Injectable
136 Dependency dependency;
137
138
139
140
141 @BeforeEach
142 void setUp() {
143 assertUtilObjectsAreAvailable();
144 }
145
146
147
148
149 void assertUtilObjectsAreAvailable() {
150 assertNotNull(util);
151 assertEquals("util", util.name);
152 assertNull(util.collaborator);
153
154 assertNotNull(util2);
155 assertEquals("util", util2.name);
156 assertNotNull(util2.collaborator);
157 }
158
159
160
161
162 @AfterEach
163 void tearDown() {
164 assertUtilObjectsAreAvailable();
165 }
166
167
168
169
170 @Test
171 void exerciseTestedObjectWithFieldInjectedByType() {
172 assertEquals(-1, tested.i);
173 assertSame(dependency, tested.dependency);
174
175 new Expectations() {
176 {
177 dependency.doSomething();
178 result = 23;
179 times = 1;
180 }
181 };
182
183 assertTrue(tested.doSomeOperation());
184 }
185
186
187
188
189
190
191
192 @Test
193 void exerciseTestedObjectCreatedThroughConstructorAndFieldInjection(@Injectable("123") int value) {
194 assertEquals(0, util.id);
195 assertEquals(123, tested.i);
196 assertSame(dependency, tested.dependency);
197 }
198
199
200
201
202
203
204
205 @Test
206 void ignoreStaticFieldsWhenDoingFieldInjection(@Injectable Runnable action) {
207 assertNull(util.action);
208 assertNull(TestedClass.globalAction);
209 }
210
211
212 @Tested("123")
213 int id;
214
215
216 @Tested
217 UtilityClass tested2;
218
219
220
221
222 @Test
223 void createTestedObjectInjectingItWithValueProvidedInPreviousTestedField() {
224 assertEquals(123, tested2.id);
225 }
226 }