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