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.assertTrue;
10
11 import java.math.BigDecimal;
12 import java.math.BigInteger;
13 import java.util.concurrent.atomic.AtomicInteger;
14 import java.util.concurrent.atomic.AtomicLong;
15
16 import mockit.integration.junit5.JMockitExtension;
17
18 import org.junit.jupiter.api.Test;
19 import org.junit.jupiter.api.extension.ExtendWith;
20
21
22
23
24 @ExtendWith(JMockitExtension.class)
25 class InjectableParameterTest {
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 @Test
50 void injectableParametersOfPrimitiveTypes(@Injectable("123") int i, @Injectable("5") long l,
51 @Injectable("-45 ") short s, @Injectable(" 127") byte b, @Injectable("true") boolean f1,
52 @Injectable(" TRUE ") boolean f2, @Injectable("A") char c, @Injectable(" 1.23") float f,
53 @Injectable("-1.23") double d) {
54 assertEquals(123, i);
55 assertEquals(5L, l);
56 assertEquals(-45, s);
57 assertEquals(127, b);
58 assertTrue(f1);
59 assertTrue(f2);
60 assertEquals('A', c);
61 assertEquals(1.23F, f, 0.0F);
62 assertEquals(-1.23, d, 0.0);
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 @Test
88 void injectableParametersOfWrapperTypes(@Injectable("123") Integer i, @Injectable("5") Long l,
89 @Injectable("-45 ") Short s, @Injectable(" 127") Byte b, @Injectable("true") Boolean f1,
90 @Injectable(" TRUE ") Boolean f2, @Injectable("A") Character c, @Injectable(" 1.23") Float f,
91 @Injectable("-1.23") Double d) {
92 assertEquals(123, (int) i);
93 assertEquals(5L, (long) l);
94 assertEquals(-45, (short) s);
95 assertEquals(127, (byte) b);
96 assertTrue(f1);
97 assertTrue(f2);
98 assertEquals('A', (char) c);
99 assertEquals(1.23F, f, 0.0F);
100 assertEquals(-1.23, d, 0.0);
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115 @Test
116 void injectableParametersOfOtherNumberSubtypes(@Injectable(" 10.234") BigDecimal d,
117 @Injectable("123 ") BigInteger i, @Injectable(" 4567 ") AtomicInteger ai,
118 @Injectable(" 23434") AtomicLong al) {
119 assertEquals(10.234, d.doubleValue(), 0.0);
120 assertEquals(123, i.intValue());
121 assertEquals(4567, ai.intValue());
122 assertEquals(23434L, al.longValue());
123 }
124
125
126
127
128 static class TestedClass {
129
130 Number n;
131
132 CharSequence text;
133
134 Comparable<Float> cmp;
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149 @Test
150 void injectablesHavingSubtypesOfTestedFields(@Tested TestedClass tested, @Injectable("test") String s,
151 @Injectable("1.2") Float cmp, @Injectable("123") Integer n) {
152 assertEquals(123, tested.n.intValue());
153 assertEquals("test", tested.text);
154 assertEquals(1.2F, tested.cmp);
155 }
156
157
158
159
160 static class TestedClass2 {
161
162
163 final Number n;
164
165
166 final CharSequence text;
167
168
169 final Comparable<Float> cmp;
170
171
172
173
174
175
176
177
178
179
180
181 TestedClass2(Number n, CharSequence text, Comparable<Float> cmp) {
182 this.n = n;
183 this.text = text;
184 this.cmp = cmp;
185 }
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200 @Test
201 void injectablesHavingSubtypesOfTestedConstructorParameters(@Tested TestedClass2 tested,
202 @Injectable("1.2") Float cmp, @Injectable("test") String s, @Injectable("123") Integer n) {
203 assertEquals(123, tested.n.intValue());
204 assertEquals("test", tested.text);
205 assertEquals(1.2F, tested.cmp);
206 }
207 }