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