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