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.assertNotSame;
6   import static org.junit.jupiter.api.Assertions.assertSame;
7   
8   import java.util.ArrayList;
9   import java.util.HashMap;
10  import java.util.List;
11  import java.util.Map;
12  
13  import javax.inject.Inject;
14  import javax.inject.Named;
15  
16  import org.junit.jupiter.api.Test;
17  
18  /**
19   * The Class TestedFieldExtractionTest.
20   */
21  final class TestedFieldExtractionTest {
22  
23      /**
24       * The Class Dependency.
25       */
26      static class Dependency {
27      }
28  
29      /**
30       * The Class TestedClassWithMultipleFieldsOfSameType.
31       */
32      static class TestedClassWithMultipleFieldsOfSameType {
33  
34          /** The dep 1. */
35          Dependency dep1;
36  
37          /** The dep 2. */
38          Dependency dep2;
39      }
40  
41      /** The tested 1. */
42      @Tested(fullyInitialized = true)
43      TestedClassWithMultipleFieldsOfSameType tested1;
44  
45      /** The dep. */
46      @Tested
47      Dependency dep;
48  
49      /**
50       * Extract multiple fields of same type into single tested field.
51       */
52      @Test
53      void extractMultipleFieldsOfSameTypeIntoSingleTestedField() {
54          assertNotNull(tested1.dep1);
55          assertNotNull(tested1.dep2);
56          assertSame(tested1.dep1, tested1.dep2); // unqualified fields of same type get the same created instance
57          assertSame(tested1.dep1, dep);
58      }
59  
60      /**
61       * The Class TestedClassWithNamedFields.
62       */
63      static class TestedClassWithNamedFields {
64  
65          /** The dep 1. */
66          @Inject
67          @Named("first")
68          Dependency dep1;
69  
70          /** The dep 2. */
71          @Inject
72          @Named("second")
73          Dependency dep2;
74      }
75  
76      /** The tested 2. */
77      @Tested(fullyInitialized = true)
78      TestedClassWithNamedFields tested2;
79  
80      /** The first. */
81      @Tested
82      Dependency first;
83  
84      /** The second. */
85      @Tested
86      Dependency second;
87  
88      /**
89       * Extract multiple qualified fields of same type into separate tested fields.
90       */
91      @Test
92      void extractMultipleQualifiedFieldsOfSameTypeIntoSeparateTestedFields() {
93          assertNotNull(tested2.dep1);
94          assertNotNull(tested2.dep2);
95          assertNotSame(tested2.dep1, tested2.dep2);
96          assertSame(tested2.dep1, first);
97          assertSame(tested2.dep2, second);
98      }
99  
100     /**
101      * The Class TestedClassWithInitializedFieldsOfVariousTypes.
102      */
103     static class TestedClassWithInitializedFieldsOfVariousTypes {
104 
105         /** The name. */
106         final String name = "test";
107 
108         /** The number. */
109         int number = 123;
110 
111         /** The names. */
112         @Inject
113         @Named("test")
114         final List<String> names = new ArrayList<>();
115 
116         /** The numbers and names. */
117         Map<Integer, String> numbersAndNames = new HashMap<>();
118     }
119 
120     /** The tested 3. */
121     @Tested
122     TestedClassWithInitializedFieldsOfVariousTypes tested3;
123 
124     /** The name. */
125     @Tested
126     String name;
127 
128     /** The test. */
129     @Tested
130     List<String> test;
131 
132     /** The numbers and names. */
133     @Tested
134     Map<Integer, String> numbersAndNames;
135 
136     /** The number. */
137     @Tested
138     int number;
139 
140     /**
141      * Extract fields initialized by constructor of tested class.
142      */
143     @Test
144     void extractFieldsInitializedByConstructorOfTestedClass() {
145         assertEquals(tested3.name, name);
146         assertEquals(tested3.number, number);
147         assertSame(tested3.names, test);
148         assertSame(tested3.numbersAndNames, numbersAndNames);
149     }
150 }