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.assertNotNull;
10  
11  import mockit.integration.junit5.ExpectedException;
12  import mockit.integration.junit5.JMockitExtension;
13  import mockit.internal.expectations.invocation.MissingInvocation;
14  
15  import org.junit.jupiter.api.Test;
16  import org.junit.jupiter.api.extension.ExtendWith;
17  
18  /**
19   * The Class ExpectationsForConstructorsTest.
20   */
21  @ExtendWith(JMockitExtension.class)
22  class ExpectationsForConstructorsTest {
23  
24      /**
25       * The Class BaseCollaborator.
26       */
27      public static class BaseCollaborator {
28  
29          /** The value. */
30          protected int value;
31  
32          /**
33           * Instantiates a new base collaborator.
34           */
35          protected BaseCollaborator() {
36              value = -1;
37          }
38  
39          /**
40           * Instantiates a new base collaborator.
41           *
42           * @param value
43           *            the value
44           */
45          protected BaseCollaborator(int value) {
46              this.value = value;
47          }
48      }
49  
50      /**
51       * The Class Collaborator.
52       */
53      static class Collaborator extends BaseCollaborator {
54  
55          /**
56           * Instantiates a new collaborator.
57           */
58          Collaborator() {
59          }
60  
61          /**
62           * Instantiates a new collaborator.
63           *
64           * @param value
65           *            the value
66           */
67          Collaborator(int value) {
68              super(value);
69          }
70      }
71  
72      /**
73       * Mock all constructors.
74       *
75       * @param unused
76       *            the unused
77       */
78      @Test
79      void mockAllConstructors(@Mocked Collaborator unused) {
80          new Expectations() {
81              {
82                  new Collaborator();
83                  new Collaborator(123);
84              }
85          };
86  
87          assertEquals(0, new Collaborator().value);
88          assertEquals(0, new Collaborator(123).value);
89      }
90  
91      /**
92       * The Class A.
93       */
94      static class A {
95  
96          /**
97           * Instantiates a new a.
98           */
99          @SuppressWarnings("UnusedDeclaration")
100         private A() {
101         }
102 
103         /**
104          * Instantiates a new a.
105          *
106          * @param s
107          *            the s
108          */
109         A(String s) {
110             assertNotNull("A(String) executed with null", s);
111         }
112     }
113 
114     /**
115      * The Class B.
116      */
117     static class B extends A {
118         /**
119          * Instantiates a new b.
120          *
121          * @param s
122          *            the s
123          */
124         B(String s) {
125             super(s);
126         }
127     }
128 
129     /**
130      * Mock class hierarchy where first constructor in base class is private.
131      *
132      * @param mock
133      *            the mock
134      */
135     @Test
136     void mockClassHierarchyWhereFirstConstructorInBaseClassIsPrivate(@Mocked B mock) {
137         new B("Test1");
138     }
139 
140     /**
141      * The Class D.
142      */
143     static class D {
144         /**
145          * Instantiates a new d.
146          *
147          * @param s
148          *            the s
149          */
150         D(@SuppressWarnings("unused") String s) {
151         }
152     }
153 
154     /**
155      * Mock class hierarchy where first constructor in base class on another package is package private.
156      *
157      * @param mock
158      *            the mock
159      */
160     @Test
161     void mockClassHierarchyWhereFirstConstructorInBaseClassOnAnotherPackageIsPackagePrivate(@Mocked D mock) {
162         assertNotNull(mock);
163         new D("Test1");
164     }
165 
166     /**
167      * The Class Base.
168      */
169     static class Base {
170     }
171 
172     /**
173      * The Class Derived.
174      */
175     static class Derived extends Base {
176     }
177 
178     /**
179      * Record and replay base constructor invocation.
180      *
181      * @param mocked
182      *            the mocked
183      */
184     @Test
185     void recordAndReplayBaseConstructorInvocation(@Mocked Base mocked) {
186         new Expectations() {
187             {
188                 new Base();
189             }
190         };
191 
192         new Base();
193     }
194 
195     /**
196      * Record expectation on base constructor and replay with call to super.
197      *
198      * @param mocked
199      *            the mocked
200      */
201     @Test
202     @ExpectedException(MissingInvocation.class)
203     void recordExpectationOnBaseConstructorAndReplayWithCallToSuper(@Mocked Base mocked) {
204         new Expectations() {
205             {
206                 new Base();
207                 times = 1;
208             }
209         };
210 
211         new Derived();
212     }
213 
214     /**
215      * Verify expectation on base constructor replayed with call to super.
216      *
217      * @param mocked
218      *            the mocked
219      */
220     @Test
221     @ExpectedException(MissingInvocation.class)
222     void verifyExpectationOnBaseConstructorReplayedWithCallToSuper(@Mocked Base mocked) {
223         new Derived();
224 
225         new Verifications() {
226             {
227                 new Base();
228             }
229         };
230     }
231 
232     /**
233      * The Class Collaborator2.
234      */
235     static class Collaborator2 {
236 
237         /**
238          * Instantiates a new collaborator 2.
239          *
240          * @param l
241          *            the l
242          */
243         Collaborator2(@SuppressWarnings("unused") long l) {
244         }
245 
246         /**
247          * Instantiates a new collaborator 2.
248          *
249          * @param c
250          *            the c
251          */
252         Collaborator2(@SuppressWarnings("unused") Collaborator2 c) {
253         }
254 
255         /**
256          * Instantiates a new collaborator 2.
257          */
258         Collaborator2() {
259             this(new Collaborator2(123L));
260         }
261     }
262 
263     /**
264      * Mock constructor which calls two others of the same class.
265      *
266      * @param mock
267      *            the mock
268      */
269     @Test
270     void mockConstructorWhichCallsTwoOthersOfTheSameClass(@Mocked Collaborator2 mock) {
271         new Collaborator2();
272     }
273 }