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