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.assertFalse;
10  import static org.junit.jupiter.api.Assertions.assertNull;
11  import static org.junit.jupiter.api.Assertions.assertTrue;
12  import static org.junit.jupiter.api.Assertions.fail;
13  
14  import java.util.List;
15  
16  import mockit.integration.junit5.JMockitExtension;
17  
18  import org.junit.jupiter.api.BeforeEach;
19  import org.junit.jupiter.api.Test;
20  import org.junit.jupiter.api.extension.ExtendWith;
21  
22  /**
23   * The Class InjectableFieldTest.
24   */
25  @ExtendWith(JMockitExtension.class)
26  class InjectableFieldTest {
27  
28      /**
29       * The Class Base.
30       */
31      static class Base {
32          /**
33           * Gets the value.
34           *
35           * @return the value
36           */
37          protected int getValue() {
38              return 1;
39          }
40      }
41  
42      /**
43       * The Class Foo.
44       */
45      static class Foo extends Base {
46  
47          /**
48           * Do something.
49           *
50           * @param s
51           *            the s
52           */
53          void doSomething(String s) {
54              throw new RuntimeException(s);
55          }
56  
57          /**
58           * Gets the another value.
59           *
60           * @return the another value
61           */
62          int getAnotherValue() {
63              return 2;
64          }
65  
66          /**
67           * Gets the boolean value.
68           *
69           * @return the boolean value
70           */
71          Boolean getBooleanValue() {
72              return true;
73          }
74  
75          /**
76           * Gets the list.
77           *
78           * @return the list
79           */
80          final List<Integer> getList() {
81              return null;
82          }
83  
84          /**
85           * Do something else.
86           *
87           * @return the string
88           */
89          static String doSomethingElse() {
90              return "";
91          }
92      }
93  
94      /** The foo. */
95      @Injectable
96      Foo foo;
97  
98      /**
99       * Record common expectations.
100      */
101     @BeforeEach
102     void recordCommonExpectations() {
103         new Expectations() {
104             {
105                 foo.getValue();
106                 result = 12;
107                 foo.getAnotherValue();
108                 result = 123;
109             }
110         };
111 
112         assertEquals(123, foo.getAnotherValue());
113         assertEquals(12, foo.getValue());
114         assertEquals(1, new Base().getValue());
115         assertEquals(2, new Foo().getAnotherValue());
116     }
117 
118     /**
119      * Cascade one level.
120      */
121     @Test
122     void cascadeOneLevel() {
123         try {
124             new Foo().doSomething("");
125             fail();
126         } catch (RuntimeException ignore) {
127         }
128 
129         new Expectations() {
130             {
131                 foo.doSomething("test");
132                 times = 1;
133             }
134         };
135 
136         assertEquals(123, foo.getAnotherValue());
137         assertFalse(foo.getBooleanValue());
138         assertTrue(foo.getList().isEmpty());
139 
140         foo.doSomething("test");
141     }
142 
143     /**
144      * Override expectation recorded in before method.
145      */
146     @Test
147     void overrideExpectationRecordedInBeforeMethod() {
148         new Expectations() {
149             {
150                 foo.getAnotherValue();
151                 result = 45;
152             }
153         };
154 
155         assertEquals(45, foo.getAnotherValue());
156         foo.doSomething("sdf");
157     }
158 
159     /**
160      * Partially mock instance without affecting injectable instances.
161      */
162     @Test
163     void partiallyMockInstanceWithoutAffectingInjectableInstances() {
164         final Foo localFoo = new Foo();
165 
166         new Expectations(localFoo) {
167             {
168                 localFoo.getAnotherValue();
169                 result = 3;
170                 Foo.doSomethingElse();
171                 result = "test";
172             }
173         };
174 
175         assertEquals(3, localFoo.getAnotherValue());
176         assertEquals(123, foo.getAnotherValue());
177         assertEquals(2, new Foo().getAnotherValue());
178         assertEquals("test", Foo.doSomethingElse());
179         foo.doSomething("");
180     }
181 
182     /** The primitive int. */
183     @Injectable
184     int primitiveInt = 123;
185 
186     /** The wrapper int. */
187     @Injectable
188     Integer wrapperInt = 45;
189 
190     /** The string. */
191     @Injectable
192     String string = "Abc";
193 
194     /**
195      * Use non mockable injectables with values provided through field assignment.
196      */
197     @Test
198     void useNonMockableInjectablesWithValuesProvidedThroughFieldAssignment() {
199         assertEquals(123, primitiveInt);
200         assertEquals(45, wrapperInt.intValue());
201         assertEquals("Abc", string);
202     }
203 
204     /** The default int. */
205     @Injectable
206     int defaultInt;
207 
208     /** The null integer. */
209     @Injectable
210     Integer nullInteger;
211 
212     /** The null string. */
213     @Injectable
214     String nullString;
215 
216     /** The empty string. */
217     @Injectable
218     String emptyString = "";
219 
220     /**
221      * Use null and empty injectables of non mockable types.
222      */
223     @Test
224     void useNullAndEmptyInjectablesOfNonMockableTypes() {
225         assertEquals(0, defaultInt);
226         assertNull(nullInteger);
227         assertNull(nullString);
228         assertTrue(emptyString.isEmpty());
229     }
230 }