View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
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   * The Class InjectableParameterTest.
23   */
24  @ExtendWith(JMockitExtension.class)
25  class InjectableParameterTest {
26  
27      /**
28       * Injectable parameters of primitive types.
29       *
30       * @param i
31       *            the i
32       * @param l
33       *            the l
34       * @param s
35       *            the s
36       * @param b
37       *            the b
38       * @param f1
39       *            the f 1
40       * @param f2
41       *            the f 2
42       * @param c
43       *            the c
44       * @param f
45       *            the f
46       * @param d
47       *            the d
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       * Injectable parameters of wrapper types.
67       *
68       * @param i
69       *            the i
70       * @param l
71       *            the l
72       * @param s
73       *            the s
74       * @param b
75       *            the b
76       * @param f1
77       *            the f 1
78       * @param f2
79       *            the f 2
80       * @param c
81       *            the c
82       * @param f
83       *            the f
84       * @param d
85       *            the d
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      * Injectable parameters of other number subtypes.
105      *
106      * @param d
107      *            the d
108      * @param i
109      *            the i
110      * @param ai
111      *            the ai
112      * @param al
113      *            the al
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      * The Class TestedClass.
127      */
128     static class TestedClass {
129         /** The n. */
130         Number n;
131         /** The text. */
132         CharSequence text;
133         /** The cmp. */
134         Comparable<Float> cmp;
135     }
136 
137     /**
138      * Injectables having subtypes of tested fields.
139      *
140      * @param tested
141      *            the tested
142      * @param s
143      *            the s
144      * @param cmp
145      *            the cmp
146      * @param n
147      *            the n
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      * The Class TestedClass2.
159      */
160     static class TestedClass2 {
161 
162         /** The n. */
163         final Number n;
164 
165         /** The text. */
166         final CharSequence text;
167 
168         /** The cmp. */
169         final Comparable<Float> cmp;
170 
171         /**
172          * Instantiates a new tested class 2.
173          *
174          * @param n
175          *            the n
176          * @param text
177          *            the text
178          * @param cmp
179          *            the cmp
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      * Injectables having subtypes of tested constructor parameters.
190      *
191      * @param tested
192      *            the tested
193      * @param cmp
194      *            the cmp
195      * @param s
196      *            the s
197      * @param n
198      *            the n
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 }