View Javadoc
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   * The Class InjectableParameterTest.
15   */
16  final class InjectableParameterTest {
17  
18      /**
19       * Injectable parameters of primitive types.
20       *
21       * @param i
22       *            the i
23       * @param l
24       *            the l
25       * @param s
26       *            the s
27       * @param b
28       *            the b
29       * @param f1
30       *            the f 1
31       * @param f2
32       *            the f 2
33       * @param c
34       *            the c
35       * @param f
36       *            the f
37       * @param d
38       *            the d
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       * Injectable parameters of wrapper types.
58       *
59       * @param i
60       *            the i
61       * @param l
62       *            the l
63       * @param s
64       *            the s
65       * @param b
66       *            the b
67       * @param f1
68       *            the f 1
69       * @param f2
70       *            the f 2
71       * @param c
72       *            the c
73       * @param f
74       *            the f
75       * @param d
76       *            the d
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       * Injectable parameters of other number subtypes.
96       *
97       * @param d
98       *            the d
99       * @param i
100      *            the i
101      * @param ai
102      *            the ai
103      * @param al
104      *            the al
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      * The Class TestedClass.
118      */
119     static class TestedClass {
120         /** The n. */
121         Number n;
122         /** The text. */
123         CharSequence text;
124         /** The cmp. */
125         Comparable<Float> cmp;
126     }
127 
128     /**
129      * Injectables having subtypes of tested fields.
130      *
131      * @param tested
132      *            the tested
133      * @param s
134      *            the s
135      * @param cmp
136      *            the cmp
137      * @param n
138      *            the n
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      * The Class TestedClass2.
150      */
151     static class TestedClass2 {
152 
153         /** The n. */
154         final Number n;
155 
156         /** The text. */
157         final CharSequence text;
158 
159         /** The cmp. */
160         final Comparable<Float> cmp;
161 
162         /**
163          * Instantiates a new tested class 2.
164          *
165          * @param n
166          *            the n
167          * @param text
168          *            the text
169          * @param cmp
170          *            the cmp
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      * Injectables having subtypes of tested constructor parameters.
181      *
182      * @param tested
183      *            the tested
184      * @param cmp
185      *            the cmp
186      * @param s
187      *            the s
188      * @param n
189      *            the n
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 }