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