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