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