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.assertNull;
10  import static org.junit.jupiter.api.Assertions.assertSame;
11  
12  import jakarta.annotation.Resource;
13  import jakarta.inject.Inject;
14  import jakarta.inject.Named;
15  
16  import java.io.Serializable;
17  
18  import javax.sql.DataSource;
19  
20  import mockit.integration.junit5.JMockitExtension;
21  
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  import org.junit.jupiter.api.extension.ExtendWith;
25  import org.springframework.beans.factory.annotation.Autowired;
26  import org.springframework.beans.factory.annotation.Qualifier;
27  
28  /**
29   * The Class TestedClassWithQualifiedDependencyTest.
30   */
31  @ExtendWith(JMockitExtension.class)
32  class TestedClassWithQualifiedDependencyTest {
33  
34      /**
35       * The Class TestedClass.
36       */
37      public static class TestedClass {
38  
39          /** The dep 1. */
40          @Inject
41          private Dependency1 dep1;
42  
43          /** The dep 2. */
44          @Autowired
45          private Dependency2 dep2;
46  
47          /** The ds. */
48          @Resource(name = "main-db")
49          private DataSource ds;
50  
51          /** The text. */
52          @Named("some.textual.value")
53          private String text;
54  
55          /** The numeric value. */
56          @Qualifier("a.BC-12")
57          private Long numericValue;
58  
59          /** The numeric value 2. */
60          @Qualifier("a.BC-12b")
61          private Long numericValue2;
62      }
63  
64      /**
65       * The Class Dependency1.
66       */
67      public static class Dependency1 {
68  
69          /** The action. */
70          @Inject
71          @Named("action1")
72          private Runnable action;
73  
74          /** The qualified dep. */
75          @Autowired
76          @Qualifier("foo")
77          private Serializable qualifiedDep;
78      }
79  
80      /**
81       * The Class Dependency2.
82       */
83      public static class Dependency2 {
84  
85          /** The action. */
86          @Qualifier("action2")
87          private Runnable action;
88  
89          /** The qualified dep. */
90          @Named("bar")
91          private Serializable qualifiedDep;
92      }
93  
94      /** The foo. */
95      @Tested
96      Serializable foo;
97  
98      /**
99       * Creates the qualified dependency.
100      */
101     @BeforeEach
102     void createQualifiedDependency() {
103         foo = "foo";
104     }
105 
106     /** The dependency 2. */
107     @Tested
108     Dependency2 dependency2;
109 
110     /** The a BC 12. */
111     @Tested
112     final Long aBC12 = 123L;
113 
114     /** The a BC 12 b. */
115     @Tested
116     final Long aBC12b = 45L;
117 
118     /** The tested. */
119     @Tested(fullyInitialized = true)
120     TestedClass tested;
121 
122     /** The action 1. */
123     @Injectable
124     Runnable action1;
125 
126     /**
127      * Use tested object with different dependencies each having A qualified sub dependency.
128      *
129      * @param action2
130      *            the action 2
131      */
132     @Test
133     void useTestedObjectWithDifferentDependenciesEachHavingAQualifiedSubDependency(@Injectable Runnable action2) {
134         assertSame(action2, dependency2.action);
135         assertSame(dependency2, tested.dep2);
136         assertSame(action1, tested.dep1.action);
137         assertSame(foo, tested.dep1.qualifiedDep);
138         assertNull(tested.dep2.qualifiedDep);
139     }
140 
141     /** The dep 1. */
142     @Tested
143     DependencyImpl dep1;
144 
145     /** The tested 2. */
146     @Tested(fullyInitialized = true)
147     ClassWithQualifiedDependencyOfAbstractType tested2;
148 
149     /**
150      * The Interface Dependency.
151      */
152     public interface Dependency {
153     }
154 
155     /**
156      * The Class DependencyImpl.
157      */
158     static class DependencyImpl implements Dependency {
159     }
160 
161     /**
162      * The Class ClassWithQualifiedDependencyOfAbstractType.
163      */
164     static class ClassWithQualifiedDependencyOfAbstractType {
165         /** The dependency. */
166         @Named("dep1")
167         Dependency dependency;
168     }
169 
170     /**
171      * Use tested object of subtype for qualified abstract dependency type in another tested object.
172      */
173     @Test
174     void useTestedObjectOfSubtypeForQualifiedAbstractDependencyTypeInAnotherTestedObject() {
175         assertSame(dep1, tested2.dependency);
176     }
177 
178     /** The main db. */
179     @Injectable
180     DataSource mainDb;
181 
182     /**
183      * Verify dependencies having composite names.
184      *
185      * @param someTextualValue
186      *            the some textual value
187      */
188     @Test
189     void verifyDependenciesHavingCompositeNames(@Injectable("text value") String someTextualValue) {
190         assertSame(mainDb, tested.ds);
191         assertEquals(someTextualValue, tested.text);
192         assertEquals(aBC12, tested.numericValue);
193         assertEquals(aBC12b, tested.numericValue2);
194     }
195 }