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.fail;
10  
11  import mockit.integration.junit5.JMockitExtension;
12  
13  import org.junit.jupiter.api.BeforeEach;
14  import org.junit.jupiter.api.Test;
15  import org.junit.jupiter.api.extension.ExtendWith;
16  
17  /**
18   * The Class FinalMockFieldsTest.
19   */
20  @ExtendWith(JMockitExtension.class)
21  class FinalMockFieldsTest {
22  
23      /**
24       * The Class Collaborator.
25       */
26      static final class Collaborator {
27  
28          /**
29           * Instantiates a new collaborator.
30           */
31          Collaborator() {
32          }
33  
34          /**
35           * Instantiates a new collaborator.
36           *
37           * @param b
38           *            the b
39           */
40          Collaborator(boolean b) {
41              if (!b) {
42                  throw new IllegalArgumentException();
43              }
44          }
45  
46          /**
47           * Gets the value.
48           *
49           * @return the value
50           */
51          int getValue() {
52              return -1;
53          }
54  
55          /**
56           * Do something.
57           */
58          void doSomething() {
59          }
60      }
61  
62      /**
63       * The Class AnotherCollaborator.
64       */
65      static final class AnotherCollaborator {
66  
67          /**
68           * Gets the value.
69           *
70           * @return the value
71           */
72          int getValue() {
73              return -1;
74          }
75  
76          /**
77           * Do something.
78           */
79          void doSomething() {
80          }
81      }
82  
83      /** The mock. */
84      @Injectable
85      final Collaborator mock = new Collaborator();
86  
87      /** The mock 2. */
88      @Mocked
89      final AnotherCollaborator mock2 = new AnotherCollaborator();
90  
91      /**
92       * Use mocked types.
93       */
94      @BeforeEach
95      void useMockedTypes() {
96          assertEquals(0, mock.getValue());
97          assertEquals(0, mock2.getValue());
98          assertEquals(0, YetAnotherCollaborator.doSomethingStatic());
99      }
100 
101     /**
102      * Record expectations on injectable final mock field.
103      */
104     @Test
105     void recordExpectationsOnInjectableFinalMockField() {
106         new Expectations() {
107             {
108                 mock.getValue();
109                 result = 12;
110                 mock.doSomething();
111                 times = 0;
112             }
113         };
114 
115         assertEquals(12, mock.getValue());
116     }
117 
118     /**
119      * Record expectations on final mock field.
120      */
121     @Test
122     void recordExpectationsOnFinalMockField() {
123         AnotherCollaborator collaborator = new AnotherCollaborator();
124 
125         new Expectations() {
126             {
127                 mock2.doSomething();
128                 times = 1;
129             }
130         };
131 
132         collaborator.doSomething();
133         assertEquals(0, collaborator.getValue());
134     }
135 
136     /** The mock process builder. */
137     @Mocked
138     final ProcessBuilder mockProcessBuilder = null;
139 
140     /**
141      * Record expectations on constructor of final mock field.
142      */
143     @Test
144     void recordExpectationsOnConstructorOfFinalMockField() {
145         new Expectations() {
146             {
147                 new ProcessBuilder("test");
148                 times = 1;
149             }
150         };
151 
152         new ProcessBuilder("test");
153     }
154 
155     /**
156      * The Class YetAnotherCollaborator.
157      */
158     static final class YetAnotherCollaborator {
159 
160         /**
161          * Instantiates a new yet another collaborator.
162          *
163          * @param b
164          *            the b
165          */
166         YetAnotherCollaborator(boolean b) {
167             if (!b) {
168                 throw new IllegalArgumentException();
169             }
170         }
171 
172         /**
173          * Gets the value.
174          *
175          * @return the value
176          */
177         int getValue() {
178             return -1;
179         }
180 
181         /**
182          * Do something.
183          */
184         void doSomething() {
185         }
186 
187         /**
188          * Do something static.
189          *
190          * @return the int
191          */
192         static int doSomethingStatic() {
193             return -2;
194         }
195     }
196 
197     /** The unused. */
198     @Mocked
199     final YetAnotherCollaborator unused = null;
200 
201     /**
202      * Record expectations on static method and constructor of final local mock field.
203      */
204     @Test
205     void recordExpectationsOnStaticMethodAndConstructorOfFinalLocalMockField() {
206         new Expectations() {
207             {
208                 new YetAnotherCollaborator(true);
209                 result = new RuntimeException();
210                 YetAnotherCollaborator.doSomethingStatic();
211                 result = 123;
212             }
213         };
214 
215         try {
216             new YetAnotherCollaborator(true);
217             fail();
218         } catch (RuntimeException ignore) {
219         }
220 
221         assertEquals(123, YetAnotherCollaborator.doSomethingStatic());
222     }
223 }