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.assertNull;
10  
11  import java.nio.CharBuffer;
12  
13  import mockit.integration.junit5.JMockitExtension;
14  
15  import org.junit.jupiter.api.Test;
16  import org.junit.jupiter.api.extension.ExtendWith;
17  
18  /**
19   * The Class MockedParametersWithCapturingTest.
20   */
21  @ExtendWith(JMockitExtension.class)
22  class MockedParametersWithCapturingTest {
23  
24      /**
25       * The Interface Service.
26       */
27      public interface Service {
28  
29          /**
30           * Do something.
31           *
32           * @return the int
33           */
34          int doSomething();
35  
36          /**
37           * Do something else.
38           *
39           * @param i
40           *            the i
41           */
42          void doSomethingElse(int i);
43      }
44  
45      /**
46       * The Class ServiceImpl.
47       */
48      static final class ServiceImpl implements Service {
49  
50          /** The str. */
51          final String str;
52  
53          /**
54           * Instantiates a new service impl.
55           *
56           * @param str
57           *            the str
58           */
59          ServiceImpl(String str) {
60              this.str = str;
61          }
62  
63          @Override
64          public int doSomething() {
65              return 1;
66          }
67  
68          @Override
69          public void doSomethingElse(int i) {
70              throw new IllegalMonitorStateException();
71          }
72      }
73  
74      /**
75       * The Class BaseClass.
76       */
77      static class BaseClass {
78  
79          /** The str. */
80          final String str;
81  
82          /**
83           * Instantiates a new base class.
84           */
85          BaseClass() {
86              str = "";
87          }
88  
89          /**
90           * Instantiates a new base class.
91           *
92           * @param str
93           *            the str
94           */
95          BaseClass(String str) {
96              this.str = str;
97          }
98  
99          /**
100          * Gets the str.
101          *
102          * @return the str
103          */
104         String getStr() {
105             return str;
106         }
107 
108         /**
109          * Do something.
110          */
111         void doSomething() {
112             throw new IllegalStateException("Invalid state");
113         }
114     }
115 
116     /**
117      * The Class DerivedClass.
118      */
119     static class DerivedClass extends BaseClass {
120 
121         /**
122          * Instantiates a new derived class.
123          */
124         DerivedClass() {
125         }
126 
127         /**
128          * Instantiates a new derived class.
129          *
130          * @param str
131          *            the str
132          */
133         DerivedClass(String str) {
134             super(str);
135         }
136 
137         @Override
138         String getStr() {
139             return super.getStr().toUpperCase();
140         }
141     }
142 
143     /**
144      * Capture derived class.
145      *
146      * @param service
147      *            the service
148      */
149     @Test
150     void captureDerivedClass(@Capturing BaseClass service) {
151         assertNull(new DerivedClass("test").str);
152         assertNull(new DerivedClass() {
153         }.str);
154     }
155 
156     /**
157      * Capture implementations of different interfaces.
158      *
159      * @param mock1
160      *            the mock 1
161      * @param mock2
162      *            the mock 2
163      *
164      * @throws Exception
165      *             the exception
166      */
167     @Test
168     void captureImplementationsOfDifferentInterfaces(@Capturing Runnable mock1, @Capturing Readable mock2)
169             throws Exception {
170         Runnable runnable = new Runnable() {
171             @Override
172             public void run() {
173                 throw new RuntimeException("run");
174             }
175         };
176         runnable.run();
177 
178         Readable readable = new Readable() {
179             @Override
180             public int read(CharBuffer cb) {
181                 throw new RuntimeException("read");
182             }
183         };
184         readable.read(CharBuffer.wrap("test"));
185     }
186 
187     /**
188      * Capture implementations of an interface.
189      *
190      * @param service
191      *            the service
192      */
193     @Test
194     void captureImplementationsOfAnInterface(@Capturing final Service service) {
195         Service impl1 = new ServiceImpl("test1");
196         impl1.doSomethingElse(1);
197 
198         Service impl2 = new Service() {
199             @Override
200             public int doSomething() {
201                 return 2;
202             }
203 
204             @Override
205             public void doSomethingElse(int i) {
206                 throw new IllegalStateException("2");
207             }
208         };
209         impl2.doSomethingElse(2);
210     }
211 
212     /**
213      * Capture subclasses of A base class.
214      *
215      * @param base
216      *            the base
217      */
218     @Test
219     void captureSubclassesOfABaseClass(@Capturing final BaseClass base) {
220         BaseClass impl1 = new DerivedClass("test1");
221         impl1.doSomething();
222 
223         BaseClass impl2 = new BaseClass("test2") {
224             @Override
225             void doSomething() {
226                 throw new IllegalStateException("2");
227             }
228         };
229         impl2.doSomething();
230 
231         final class DerivedClass2 extends DerivedClass {
232             DerivedClass2() {
233                 super("DeRiVed");
234             }
235 
236             @Override
237             String getStr() {
238                 return super.getStr().toLowerCase();
239             }
240         }
241         DerivedClass2 impl3 = new DerivedClass2();
242         impl3.doSomething();
243     }
244 
245     /**
246      * The Interface IBase.
247      */
248     public interface IBase {
249         /**
250          * Do something.
251          *
252          * @return the int
253          */
254         int doSomething();
255     }
256 
257     /**
258      * The Interface ISub.
259      */
260     public interface ISub extends IBase {
261     }
262 
263     /**
264      * Record call to base interface method on capture sub interface implementation.
265      *
266      * @param mock
267      *            the mock
268      */
269     @Test
270     void recordCallToBaseInterfaceMethodOnCaptureSubInterfaceImplementation(@Capturing final ISub mock) {
271         new Expectations() {
272             {
273                 mock.doSomething();
274                 result = 123;
275             }
276         };
277 
278         ISub impl = new ISub() {
279             @Override
280             public int doSomething() {
281                 return -1;
282             }
283         };
284         int i = impl.doSomething();
285 
286         assertEquals(123, i);
287     }
288 }