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.assertArrayEquals;
9   import static org.junit.jupiter.api.Assertions.assertEquals;
10  import static org.junit.jupiter.api.Assertions.assertSame;
11  import static org.junit.jupiter.api.Assertions.assertTrue;
12  
13  import mockit.integration.junit5.ExpectedException;
14  import mockit.integration.junit5.JMockitExtension;
15  
16  import org.junit.jupiter.api.Test;
17  import org.junit.jupiter.api.extension.ExtendWith;
18  
19  /**
20   * The Class TestedClassInjectedFromMockParametersTest.
21   */
22  @ExtendWith(JMockitExtension.class)
23  class TestedClassInjectedFromMockParametersTest {
24  
25      /**
26       * The Enum AnEnum.
27       */
28      enum AnEnum {
29          /** The Abc. */
30          Abc,
31          /** The Xyz. */
32          Xyz
33      }
34  
35      /**
36       * The Class TestedClass.
37       */
38      public static final class TestedClass {
39  
40          /** The i. */
41          private int i;
42  
43          /** The s. */
44          private String s;
45  
46          /** The b. */
47          private boolean b;
48  
49          /** The chars. */
50          private char[] chars;
51  
52          /** The enum value. */
53          AnEnum enumValue;
54  
55          /**
56           * Instantiates a new tested class.
57           *
58           * @param b
59           *            the b
60           */
61          public TestedClass(boolean b) {
62              this.b = b;
63          }
64  
65          /**
66           * Instantiates a new tested class.
67           *
68           * @param i
69           *            the i
70           * @param s
71           *            the s
72           * @param b
73           *            the b
74           * @param chars
75           *            the chars
76           */
77          public TestedClass(int i, String s, boolean b, char... chars) {
78              this.i = i;
79              this.s = s;
80              this.b = b;
81              this.chars = chars;
82          }
83  
84          /**
85           * Instantiates a new tested class.
86           *
87           * @param b1
88           *            the b 1
89           * @param b2
90           *            the b 2
91           * @param b3
92           *            the b 3
93           */
94          public TestedClass(boolean b1, byte b2, boolean b3) {
95              b = b1;
96              chars = new char[] { (char) b2, b3 ? 'X' : 'x' };
97          }
98  
99          /**
100          * Instantiates a new tested class.
101          *
102          * @param first
103          *            the first
104          * @param second
105          *            the second
106          * @param third
107          *            the third
108          */
109         public TestedClass(char first, char second, char third) {
110             chars = new char[] { first, second, third };
111         }
112     }
113 
114     /** The tested. */
115     @Tested
116     TestedClass tested;
117 
118     /**
119      * Attempt to instantiate tested class with no injectables.
120      */
121     @Test
122     @ExpectedException(IllegalArgumentException.class)
123     void attemptToInstantiateTestedClassWithNoInjectables() {
124     }
125 
126     /**
127      * Instantiate tested object from mock parameters using one constructor.
128      *
129      * @param s
130      *            the s
131      * @param mock1
132      *            the mock 1
133      * @param mock2
134      *            the mock 2
135      * @param c1
136      *            the c 1
137      * @param c2
138      *            the c 2
139      */
140     @Test
141     void instantiateTestedObjectFromMockParametersUsingOneConstructor(@Injectable("Text") String s,
142             @Injectable("123") int mock1, @Injectable("true") boolean mock2, @Injectable("A") char c1,
143             @Injectable("bB") char c2) {
144         assertEquals("Text", s);
145         assertEquals(s, tested.s);
146         assertEquals(mock1, tested.i);
147         assertEquals(mock2, tested.b);
148         assertEquals(2, tested.chars.length);
149         assertEquals(c1, tested.chars[0]);
150         assertEquals(c2, tested.chars[1]);
151         assertEquals('b', c2);
152     }
153 
154     /**
155      * Instantiate tested object from mock parameters using another constructor.
156      *
157      * @param b1
158      *            the b 1
159      * @param b3
160      *            the b 3
161      * @param b2
162      *            the b 2
163      */
164     @Test
165     void instantiateTestedObjectFromMockParametersUsingAnotherConstructor(@Injectable("true") boolean b1,
166             @Injectable("true") boolean b3, @Injectable("65") byte b2) {
167         assertTrue(tested.b);
168         assertEquals('A', tested.chars[0]);
169         assertEquals('X', tested.chars[1]);
170     }
171 
172     /**
173      * Instantiate tested object using constructor with multiple parameters of the same type matched by name.
174      *
175      * @param second
176      *            the second
177      * @param third
178      *            the third
179      * @param first
180      *            the first
181      */
182     @Test
183     void instantiateTestedObjectUsingConstructorWithMultipleParametersOfTheSameTypeMatchedByName(
184             @Injectable("S") char second, @Injectable("T") char third, @Injectable("F") char first) {
185         assertArrayEquals(new char[] { 'F', 'S', 'T' }, tested.chars);
186     }
187 
188     /**
189      * Sets the enum field in tested object from value provided in parameter.
190      *
191      * @param flag
192      *            the flag
193      * @param enumVal
194      *            the enum val
195      */
196     @Test
197     void setEnumFieldInTestedObjectFromValueProvidedInParameter(@Injectable("false") boolean flag,
198             @Injectable("Xyz") AnEnum enumVal) {
199         assertSame(AnEnum.Xyz, tested.enumValue);
200     }
201 }