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