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.assertNotNull;
10  import static org.junit.jupiter.api.Assertions.assertNull;
11  import static org.junit.jupiter.api.Assertions.assertSame;
12  import static org.junit.jupiter.api.Assertions.assertTrue;
13  
14  import mockit.integration.junit5.JMockitExtension;
15  
16  import org.junit.jupiter.api.AfterEach;
17  import org.junit.jupiter.api.BeforeEach;
18  import org.junit.jupiter.api.Test;
19  import org.junit.jupiter.api.extension.ExtendWith;
20  
21  /**
22   * The Class TestedClassWithFieldDITest.
23   */
24  @ExtendWith(JMockitExtension.class)
25  class TestedClassWithFieldDITest {
26  
27      /** The tested global. */
28      @Tested
29      TestedClassInGlobalScopeTest.TestedClass testedGlobal;
30  
31      /**
32       * Use local tested field having same type and name as global tested field in previous test class.
33       */
34      @Test
35      void useLocalTestedFieldHavingSameTypeAndNameAsGlobalTestedFieldInPreviousTestClass() {
36          assertNull(testedGlobal.someValue);
37      }
38  
39      /**
40       * The Class UtilityClass.
41       */
42      static final class UtilityClass {
43  
44          /** The name. */
45          String name;
46  
47          /** The id. */
48          int id;
49  
50          /** The action. */
51          Runnable action;
52  
53          /** The collaborator. */
54          Collaborator collaborator;
55      }
56  
57      /**
58       * The Class TestedClass.
59       */
60      public static class TestedClass {
61  
62          /** The global action. */
63          static Runnable globalAction;
64  
65          /** The i. */
66          protected final int i;
67  
68          /** The dependency. */
69          protected Dependency dependency;
70  
71          /**
72           * Instantiates a new tested class.
73           */
74          public TestedClass() {
75              i = -1;
76          }
77  
78          /**
79           * Instantiates a new tested class.
80           *
81           * @param i
82           *            the i
83           */
84          public TestedClass(int i) {
85              this.i = i;
86          }
87  
88          /**
89           * Do some operation.
90           *
91           * @return true, if successful
92           */
93          public boolean doSomeOperation() {
94              return dependency.doSomething() > 0;
95          }
96      }
97  
98      /**
99       * The Class Dependency.
100      */
101     static class Dependency {
102         /**
103          * Do something.
104          *
105          * @return the int
106          */
107         int doSomething() {
108             return -1;
109         }
110     }
111 
112     /**
113      * The Class Collaborator.
114      */
115     public static class Collaborator {
116     }
117 
118     /** The util. */
119     @Tested(availableDuringSetup = true)
120     UtilityClass util;
121 
122     /** The util 2. */
123     @Tested(availableDuringSetup = true, fullyInitialized = true)
124     UtilityClass util2;
125 
126     /** The util name. */
127     @Injectable("util")
128     String utilName;
129 
130     /** The tested. */
131     @Tested
132     TestedClass tested;
133 
134     /** The dependency. */
135     @Injectable
136     Dependency dependency;
137 
138     /**
139      * Sets the up.
140      */
141     @BeforeEach
142     void setUp() {
143         assertUtilObjectsAreAvailable();
144     }
145 
146     /**
147      * Assert util objects are available.
148      */
149     void assertUtilObjectsAreAvailable() {
150         assertNotNull(util);
151         assertEquals("util", util.name);
152         assertNull(util.collaborator);
153 
154         assertNotNull(util2);
155         assertEquals("util", util2.name);
156         assertNotNull(util2.collaborator);
157     }
158 
159     /**
160      * Tear down.
161      */
162     @AfterEach
163     void tearDown() {
164         assertUtilObjectsAreAvailable();
165     }
166 
167     /**
168      * Exercise tested object with field injected by type.
169      */
170     @Test
171     void exerciseTestedObjectWithFieldInjectedByType() {
172         assertEquals(-1, tested.i);
173         assertSame(dependency, tested.dependency);
174 
175         new Expectations() {
176             {
177                 dependency.doSomething();
178                 result = 23;
179                 times = 1;
180             }
181         };
182 
183         assertTrue(tested.doSomeOperation());
184     }
185 
186     /**
187      * Exercise tested object created through constructor and field injection.
188      *
189      * @param value
190      *            the value
191      */
192     @Test
193     void exerciseTestedObjectCreatedThroughConstructorAndFieldInjection(@Injectable("123") int value) {
194         assertEquals(0, util.id);
195         assertEquals(123, tested.i);
196         assertSame(dependency, tested.dependency);
197     }
198 
199     /**
200      * Ignore static fields when doing field injection.
201      *
202      * @param action
203      *            the action
204      */
205     @Test
206     void ignoreStaticFieldsWhenDoingFieldInjection(@Injectable Runnable action) {
207         assertNull(util.action);
208         assertNull(TestedClass.globalAction);
209     }
210 
211     /** The id. */
212     @Tested("123")
213     int id;
214 
215     /** The tested 2. */
216     @Tested
217     UtilityClass tested2;
218 
219     /**
220      * Creates the tested object injecting it with value provided in previous tested field.
221      */
222     @Test
223     void createTestedObjectInjectingItWithValueProvidedInPreviousTestedField() {
224         assertEquals(123, tested2.id);
225     }
226 }