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.assertNotNull;
9   import static org.junit.jupiter.api.Assertions.assertSame;
10  import static org.junit.jupiter.api.Assertions.assertTrue;
11  
12  import mockit.integration.junit5.JMockitExtension;
13  
14  import org.junit.jupiter.api.Test;
15  import org.junit.jupiter.api.extension.ExtendWith;
16  
17  /**
18   * The Class TestedClassWithGenericSubtypesTest.
19   */
20  @ExtendWith(JMockitExtension.class)
21  class TestedClassWithGenericSubtypesTest {
22  
23      /**
24       * The Class GenericClass.
25       *
26       * @param <T>
27       *            the generic type
28       */
29      static class GenericClass<T> {
30          /** The value. */
31          T value;
32      }
33  
34      /**
35       * The Class Subclass1.
36       */
37      static class Subclass1 extends GenericClass<String> {
38      }
39  
40      /**
41       * The Class Subclass2.
42       */
43      static class Subclass2 extends GenericClass<Double> {
44      }
45  
46      /**
47       * The Class SUT1.
48       */
49      static class SUT1 {
50  
51          /** The dependency 1. */
52          GenericClass<String> dependency1;
53  
54          /** The dependency 2. */
55          GenericClass<Double> dependency2;
56      }
57  
58      /**
59       * Inject subclass instances into fields of base generic class.
60       *
61       * @param sut
62       *            the sut
63       * @param s1
64       *            the s 1
65       * @param s2
66       *            the s 2
67       */
68      @Test
69      void injectSubclassInstancesIntoFieldsOfBaseGenericClass(@Tested SUT1 sut, @Injectable Subclass1 s1,
70              @Injectable Subclass2 s2) {
71          assertSame(s1, sut.dependency1);
72          assertSame(s2, sut.dependency2);
73      }
74  
75      /**
76       * The Interface GenericInterface.
77       *
78       * @param <T>
79       *            the generic type
80       */
81      @SuppressWarnings("unused")
82      public interface GenericInterface<T> {
83      }
84  
85      /**
86       * The Class Impl1.
87       */
88      static class Impl1 implements GenericInterface<String> {
89      }
90  
91      /**
92       * The Class Impl2.
93       */
94      static class Impl2 implements GenericInterface<Double> {
95      }
96  
97      /**
98       * The Class SUT2.
99       */
100     static class SUT2 {
101 
102         /** The dependency 1. */
103         final GenericInterface<String> dependency1;
104 
105         /** The dependency 2. */
106         final GenericInterface<Double> dependency2;
107 
108         /**
109          * Instantiates a new sut2.
110          *
111          * @param dep1
112          *            the dep 1
113          * @param dep2
114          *            the dep 2
115          */
116         SUT2(GenericInterface<String> dep1, GenericInterface<Double> dep2) {
117             dependency1 = dep1;
118             dependency2 = dep2;
119         }
120     }
121 
122     /**
123      * Inject implementation instances into fields of base generic interface.
124      *
125      * @param sut
126      *            the sut
127      * @param i1
128      *            the i 1
129      * @param i2
130      *            the i 2
131      */
132     @Test
133     void injectImplementationInstancesIntoFieldsOfBaseGenericInterface(@Tested SUT2 sut, @Injectable Impl1 i1,
134             @Injectable Impl2 i2) {
135         assertSame(i1, sut.dependency1);
136         assertSame(i2, sut.dependency2);
137     }
138 
139     /**
140      * The Class Dependency.
141      */
142     static final class Dependency {
143     }
144 
145     /**
146      * The Class Service1.
147      */
148     static final class Service1 extends GenericClass<Dependency> {
149     }
150 
151     /**
152      * The Class Service2.
153      */
154     static final class Service2 {
155         /** The service 1. */
156         Service1 service1;
157     }
158 
159     /**
160      * Inject instance into type variable of second level class.
161      *
162      * @param service2
163      *            the service 2
164      */
165     @Test
166     void injectInstanceIntoTypeVariableOfSecondLevelClass(@Tested(fullyInitialized = true) Service2 service2) {
167         Service1 service1 = service2.service1;
168         assertNotNull(service1);
169         assertTrue(service1.value instanceof Dependency);
170     }
171 }