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