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.assertEquals;
11  import static org.junit.jupiter.api.Assertions.assertFalse;
12  
13  import jakarta.enterprise.inject.Instance;
14  import jakarta.inject.Inject;
15  
16  import java.util.ArrayList;
17  import java.util.HashSet;
18  import java.util.List;
19  import java.util.Set;
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 InstanceDITest.
28   */
29  @ExtendWith(JMockitExtension.class)
30  class InstanceDITest {
31  
32      /**
33       * The Class Collaborator.
34       */
35      static class Collaborator {
36      }
37  
38      /**
39       * The Class TestedClassWithInstanceInjectionPoints.
40       */
41      static final class TestedClassWithInstanceInjectionPoints {
42  
43          /** The names. */
44          final Set<String> names;
45  
46          /** The collaborators. */
47          @Inject
48          Instance<Collaborator> collaborators;
49  
50          /**
51           * Instantiates a new tested class with instance injection points.
52           *
53           * @param names
54           *            the names
55           */
56          @Inject
57          TestedClassWithInstanceInjectionPoints(Instance<String> names) {
58              this.names = new HashSet<>();
59  
60              for (String name : names) {
61                  this.names.add(name);
62              }
63          }
64      }
65  
66      /** The tested. */
67      @Tested
68      TestedClassWithInstanceInjectionPoints tested;
69  
70      /** The col 1. */
71      @Injectable
72      Collaborator col1;
73  
74      /** The col 2. */
75      @Injectable
76      Collaborator col2;
77  
78      /** The names. */
79      @Injectable
80      final Iterable<String> names = asList("Abc", "Test", "123");
81  
82      /**
83       * Allow multiple injectables of same type to be obtained from instance injection point.
84       */
85      @Test
86      void allowMultipleInjectablesOfSameTypeToBeObtainedFromInstanceInjectionPoint() {
87          assertEquals(new HashSet<>(asList("Abc", "Test", "123")), tested.names);
88  
89          Instance<Collaborator> collaborators = tested.collaborators;
90          assertFalse(collaborators.isAmbiguous());
91          assertFalse(collaborators.isUnsatisfied());
92  
93          List<Collaborator> collaboratorInstances = toList(collaborators);
94          assertEquals(asList(col1, col2), collaboratorInstances);
95      }
96  
97      /**
98       * To list.
99       *
100      * @param <T>
101      *            the generic type
102      * @param instances
103      *            the instances
104      *
105      * @return the list
106      */
107     static <T> List<T> toList(Iterable<T> instances) {
108         List<T> list = new ArrayList<>();
109 
110         for (T instance : instances) {
111             list.add(instance);
112         }
113 
114         return list;
115     }
116 }