View Javadoc
1   package mockit;
2   
3   import static java.util.Arrays.asList;
4   
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 java.util.Collection;
10  import java.util.List;
11  import java.util.Set;
12  
13  import javax.inject.Inject;
14  
15  import org.junit.jupiter.api.Test;
16  
17  /**
18   * The Class IterableDITest.
19   */
20  final class IterableDITest {
21  
22      /**
23       * The Class Collaborator.
24       */
25      static class Collaborator {
26  
27          /** The value. */
28          final int value;
29  
30          /**
31           * Instantiates a new collaborator.
32           */
33          Collaborator() {
34              value = 0;
35          }
36  
37          /**
38           * Instantiates a new collaborator.
39           *
40           * @param value
41           *            the value
42           */
43          Collaborator(int value) {
44              this.value = value;
45          }
46      }
47  
48      /**
49       * The Class TestedClassWithIterableInjectionPoints.
50       */
51      static final class TestedClassWithIterableInjectionPoints {
52  
53          /** The names. */
54          final List<String> names;
55  
56          /** The collaborators. */
57          @Inject
58          Collection<Collaborator> collaborators;
59  
60          /** The numbers. */
61          Set<? extends Number> numbers;
62  
63          /**
64           * Instantiates a new tested class with iterable injection points.
65           *
66           * @param names
67           *            the names
68           */
69          @Inject
70          TestedClassWithIterableInjectionPoints(List<String> names) {
71              this.names = names;
72          }
73      }
74  
75      /** The name list. */
76      @Injectable
77      final List<String> nameList = asList("One", "Two");
78  
79      /** The col list. */
80      @Injectable
81      final Collection<Collaborator> colList = asList(new Collaborator(1), new Collaborator(2));
82  
83      /** The tested 1. */
84      @Tested
85      TestedClassWithIterableInjectionPoints tested1;
86  
87      /**
88       * Inject multi valued injectables into injection points of the same collection types.
89       */
90      @Test
91      void injectMultiValuedInjectablesIntoInjectionPointsOfTheSameCollectionTypes() {
92          assertSame(nameList, tested1.names);
93          assertSame(colList, tested1.collaborators);
94          assertNull(tested1.numbers);
95      }
96  
97      /**
98       * The Class Dependency.
99       */
100     static class Dependency {
101     }
102 
103     /**
104      * The Class SubDependency.
105      */
106     static class SubDependency extends Dependency {
107     }
108 
109     /**
110      * The Class TestedClassWithInjectedList.
111      */
112     static class TestedClassWithInjectedList {
113 
114         /** The dependencies. */
115         @Inject
116         List<Dependency> dependencies;
117 
118         /** The names. */
119         Set<String> names;
120     }
121 
122     /** The tested 2. */
123     @Tested
124     TestedClassWithInjectedList tested2;
125 
126     /** The dependency. */
127     @Injectable
128     Dependency dependency;
129 
130     /**
131      * Inject mocked instance into list.
132      */
133     @Test
134     void injectMockedInstanceIntoList() {
135         assertTrue(tested2.dependencies.contains(dependency));
136     }
137 
138     /**
139      * Do not inject string into unannotated set.
140      *
141      * @param name
142      *            the name
143      */
144     @Test
145     void doNotInjectStringIntoUnannotatedSet(@Injectable("test") String name) {
146         assertNull(tested2.names);
147     }
148 
149     /**
150      * Inject sub type instance into list of base type.
151      *
152      * @param sub
153      *            the sub
154      */
155     @Test
156     void injectSubTypeInstanceIntoListOfBaseType(@Injectable SubDependency sub) {
157         assertTrue(tested2.dependencies.contains(sub));
158     }
159 }