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.assertNotNull;
11 import static org.junit.jupiter.api.Assertions.assertNull;
12 import static org.junit.jupiter.api.Assertions.assertSame;
13
14 import mockit.integration.junit5.JMockitExtension;
15
16 import org.junit.jupiter.api.Test;
17 import org.junit.jupiter.api.extension.ExtendWith;
18
19 class BaseTest {
20 static class Dependency {
21 int doSomething() {
22 return -1;
23 }
24 }
25
26 static class Collaborator {
27 }
28
29 public static class TestedClass {
30 protected final int i;
31 protected Dependency dependency;
32 Runnable action;
33
34 public TestedClass() {
35 i = -1;
36 }
37
38 public TestedClass(int i) {
39 this.i = i;
40 }
41
42 public boolean doSomeOperation() {
43 return dependency.doSomething() > 0;
44 }
45 }
46
47 @Tested
48 TestedClass tested1;
49 @Injectable
50 Dependency dependency;
51 @Tested
52 Collaborator collaborator;
53
54 final void verifyTestedObjectFromBaseTestClass(int expectedValueForIntField) {
55 assertEquals(expectedValueForIntField, tested1.i);
56 assertSame(dependency, tested1.dependency);
57 assertNotNull(tested1.action);
58 }
59 }
60
61
62
63
64 @ExtendWith(JMockitExtension.class)
65 class TestedClassWithConstructorAndFieldDITest extends BaseTest {
66
67
68
69
70 @SuppressWarnings("unused")
71 public static class AnotherTestedClass extends TestedClass {
72
73
74 Runnable anotherAction;
75
76
77 Dependency dependency3;
78
79
80 Dependency dependency2;
81
82
83
84
85 public AnotherTestedClass() {
86 super(-2);
87 }
88
89
90
91
92
93
94
95
96
97 public AnotherTestedClass(int value, Dependency dependency1) {
98 super(value);
99
100 super.dependency = dependency1;
101 }
102
103 @Override
104 public boolean doSomeOperation() {
105 boolean b = dependency2.doSomething() > 0;
106 return super.doSomeOperation() && b;
107 }
108 }
109
110
111 @Tested
112 AnotherTestedClass tested2;
113
114
115 @Injectable
116 Runnable anotherAction;
117
118
119 @Injectable
120 Dependency dependency2;
121
122
123
124
125 @Test
126 void exerciseTestedSubclassObjectWithFieldsInjectedByTypeAndName() {
127 verifyTestedObjectFromBaseTestClass(-1);
128
129 assertEquals(-2, tested2.i);
130 assertSame(anotherAction, tested2.anotherAction);
131 assertSame(dependency, tested2.dependency);
132 assertSame(dependency2, tested2.dependency2);
133 assertNull(tested2.dependency3);
134 assertFalse(tested2.doSomeOperation());
135
136 new Verifications() {
137 {
138 anotherAction.run();
139 times = 0;
140 dependency.doSomething();
141 times = 1;
142 dependency2.doSomething();
143 }
144 };
145 }
146
147
148
149
150
151
152
153 @Test
154 void exerciseTestedSubclassObjectWithFieldsInjectedFromMockFieldsAndMockParameter(
155 @Injectable Dependency dependency3) {
156 verifyTestedObjectFromBaseTestClass(-1);
157
158 assertEquals(-2, tested2.i);
159 assertSame(dependency, tested2.dependency);
160 assertSame(dependency2, tested2.dependency2);
161 assertSame(dependency3, tested2.dependency3);
162 assertSame(anotherAction, tested2.anotherAction);
163 assertFalse(tested2.doSomeOperation());
164 }
165
166
167
168
169
170
171
172
173
174 @Test
175 void exerciseTestedSubclassObjectUsingConstructorAndFieldInjection(@Injectable("45") int value,
176 @Injectable Dependency dependency1) {
177 verifyTestedObjectFromBaseTestClass(45);
178
179 assertEquals(45, tested2.i);
180 assertSame(dependency1, tested2.dependency);
181 assertSame(dependency2, tested2.dependency2);
182 assertNull(tested2.dependency3);
183 assertSame(anotherAction, tested2.anotherAction);
184 assertFalse(tested2.doSomeOperation());
185 }
186
187
188
189
190 static class ClassWithFieldHavingTestedFieldInBaseTestClass {
191
192 Collaborator collaborator;
193 }
194
195
196 @Tested
197 ClassWithFieldHavingTestedFieldInBaseTestClass tested3;
198
199
200
201
202
203
204
205 @Test
206 void createTestedParameterInjectingFromTestedFieldInBaseTestClass(
207 @Tested ClassWithFieldHavingTestedFieldInBaseTestClass tested4) {
208 assertNotNull(collaborator);
209 assertSame(collaborator, tested3.collaborator);
210 assertSame(collaborator, tested4.collaborator);
211 }
212 }