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