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
11 import mockit.integration.junit5.JMockitExtension;
12
13 import org.junit.jupiter.api.Test;
14 import org.junit.jupiter.api.extension.ExtendWith;
15
16
17
18
19 @ExtendWith(JMockitExtension.class)
20 class TestedClassWithConstructorDI3Test {
21
22
23
24
25 public static final class TestedClass {
26
27
28 private final Dependency[] dependencies;
29
30
31
32
33
34
35
36
37
38 public TestedClass(Runnable r, Dependency... dependencies) {
39 this.dependencies = dependencies;
40 r.run();
41 }
42
43
44
45
46
47
48 public int doSomeOperation() {
49 int sum = 0;
50
51 for (Dependency dependency : dependencies) {
52 sum += dependency.doSomething();
53 }
54
55 return sum;
56 }
57 }
58
59
60
61
62 static class Dependency {
63
64
65
66
67
68
69 int doSomething() {
70 return -1;
71 }
72 }
73
74
75 @Tested(availableDuringSetup = true)
76 TestedClass support;
77
78
79 @Tested
80 TestedClass tested;
81
82
83 @Injectable
84 Dependency mock1;
85
86
87 @Injectable
88 Runnable task;
89
90
91 @Injectable
92 Dependency mock2;
93
94
95
96
97 @Test
98 void exerciseTestedObjectWithDependenciesOfSameTypeInjectedThroughVarargsConstructorParameter() {
99 assertNotNull(support);
100
101 new Expectations() {
102 {
103 mock1.doSomething();
104 result = 23;
105 mock2.doSomething();
106 result = 5;
107 }
108 };
109
110 assertEquals(28, tested.doSomeOperation());
111 }
112
113
114
115
116
117
118
119 @Test
120 void exerciseTestedObjectWithDependenciesProvidedByMockFieldsAndMockParameter(@Injectable final Dependency mock3) {
121 assertNotNull(support);
122
123 new Expectations() {
124 {
125 mock1.doSomething();
126 result = 2;
127 mock2.doSomething();
128 result = 3;
129 mock3.doSomething();
130 result = 5;
131 }
132 };
133
134 assertEquals(10, tested.doSomeOperation());
135 }
136
137
138
139
140 static class ClassWithStringParameter {
141
142
143 final String name;
144
145
146
147
148
149
150
151 ClassWithStringParameter(String name) {
152 this.name = name;
153 }
154 }
155
156
157 @Tested
158 ClassWithStringParameter tested2;
159
160
161 @Injectable
162 String name;
163
164
165
166
167 @Test
168 void initializeTestedObjectWithEmptyStringParameter() {
169 assertEquals("", tested2.name);
170 }
171 }