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