1
2
3
4
5
6 package mockit;
7
8 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
9 import static org.junit.jupiter.api.Assertions.assertEquals;
10 import static org.junit.jupiter.api.Assertions.assertSame;
11 import static org.junit.jupiter.api.Assertions.assertTrue;
12
13 import mockit.integration.junit5.ExpectedException;
14 import mockit.integration.junit5.JMockitExtension;
15
16 import org.junit.jupiter.api.Test;
17 import org.junit.jupiter.api.extension.ExtendWith;
18
19
20
21
22 @ExtendWith(JMockitExtension.class)
23 class TestedClassInjectedFromMockParametersTest {
24
25
26
27
28 enum AnEnum {
29
30 Abc,
31
32 Xyz
33 }
34
35
36
37
38 public static final class TestedClass {
39
40
41 private int i;
42
43
44 private String s;
45
46
47 private boolean b;
48
49
50 private char[] chars;
51
52
53 AnEnum enumValue;
54
55
56
57
58
59
60
61 public TestedClass(boolean b) {
62 this.b = b;
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public TestedClass(int i, String s, boolean b, char... chars) {
78 this.i = i;
79 this.s = s;
80 this.b = b;
81 this.chars = chars;
82 }
83
84
85
86
87
88
89
90
91
92
93
94 public TestedClass(boolean b1, byte b2, boolean b3) {
95 b = b1;
96 chars = new char[] { (char) b2, b3 ? 'X' : 'x' };
97 }
98
99
100
101
102
103
104
105
106
107
108
109 public TestedClass(char first, char second, char third) {
110 chars = new char[] { first, second, third };
111 }
112 }
113
114
115 @Tested
116 TestedClass tested;
117
118
119
120
121 @Test
122 @ExpectedException(IllegalArgumentException.class)
123 void attemptToInstantiateTestedClassWithNoInjectables() {
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 @Test
141 void instantiateTestedObjectFromMockParametersUsingOneConstructor(@Injectable("Text") String s,
142 @Injectable("123") int mock1, @Injectable("true") boolean mock2, @Injectable("A") char c1,
143 @Injectable("bB") char c2) {
144 assertEquals("Text", s);
145 assertEquals(s, tested.s);
146 assertEquals(mock1, tested.i);
147 assertEquals(mock2, tested.b);
148 assertEquals(2, tested.chars.length);
149 assertEquals(c1, tested.chars[0]);
150 assertEquals(c2, tested.chars[1]);
151 assertEquals('b', c2);
152 }
153
154
155
156
157
158
159
160
161
162
163
164 @Test
165 void instantiateTestedObjectFromMockParametersUsingAnotherConstructor(@Injectable("true") boolean b1,
166 @Injectable("true") boolean b3, @Injectable("65") byte b2) {
167 assertTrue(tested.b);
168 assertEquals('A', tested.chars[0]);
169 assertEquals('X', tested.chars[1]);
170 }
171
172
173
174
175
176
177
178
179
180
181
182 @Test
183 void instantiateTestedObjectUsingConstructorWithMultipleParametersOfTheSameTypeMatchedByName(
184 @Injectable("S") char second, @Injectable("T") char third, @Injectable("F") char first) {
185 assertArrayEquals(new char[] { 'F', 'S', 'T' }, tested.chars);
186 }
187
188
189
190
191
192
193
194
195
196 @Test
197 void setEnumFieldInTestedObjectFromValueProvidedInParameter(@Injectable("false") boolean flag,
198 @Injectable("Xyz") AnEnum enumVal) {
199 assertSame(AnEnum.Xyz, tested.enumValue);
200 }
201 }