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