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