1
2
3
4
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
19
20 @ExtendWith(JMockitExtension.class)
21 class TestedClassWithGenericSubtypesTest {
22
23
24
25
26
27
28
29 static class GenericClass<T> {
30
31 T value;
32 }
33
34
35
36
37 static class Subclass1 extends GenericClass<String> {
38 }
39
40
41
42
43 static class Subclass2 extends GenericClass<Double> {
44 }
45
46
47
48
49 static class SUT1 {
50
51
52 GenericClass<String> dependency1;
53
54
55 GenericClass<Double> dependency2;
56 }
57
58
59
60
61
62
63
64
65
66
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
77
78
79
80
81 @SuppressWarnings("unused")
82 public interface GenericInterface<T> {
83 }
84
85
86
87
88 static class Impl1 implements GenericInterface<String> {
89 }
90
91
92
93
94 static class Impl2 implements GenericInterface<Double> {
95 }
96
97
98
99
100 static class SUT2 {
101
102
103 final GenericInterface<String> dependency1;
104
105
106 final GenericInterface<Double> dependency2;
107
108
109
110
111
112
113
114
115
116 SUT2(GenericInterface<String> dep1, GenericInterface<Double> dep2) {
117 dependency1 = dep1;
118 dependency2 = dep2;
119 }
120 }
121
122
123
124
125
126
127
128
129
130
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
141
142 static final class Dependency {
143 }
144
145
146
147
148 static final class Service1 extends GenericClass<Dependency> {
149 }
150
151
152
153
154 static final class Service2 {
155
156 Service1 service1;
157 }
158
159
160
161
162
163
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 }