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