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
11
12 final class TestedClassWithGenericSubtypesTest {
13
14
15
16
17
18
19
20 static class GenericClass<T> {
21
22 T value;
23 }
24
25
26
27
28 static class Subclass1 extends GenericClass<String> {
29 }
30
31
32
33
34 static class Subclass2 extends GenericClass<Double> {
35 }
36
37
38
39
40 static class SUT1 {
41
42
43 GenericClass<String> dependency1;
44
45
46 GenericClass<Double> dependency2;
47 }
48
49
50
51
52
53
54
55
56
57
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
68
69
70
71
72 @SuppressWarnings("unused")
73 public interface GenericInterface<T> {
74 }
75
76
77
78
79 static class Impl1 implements GenericInterface<String> {
80 }
81
82
83
84
85 static class Impl2 implements GenericInterface<Double> {
86 }
87
88
89
90
91 static class SUT2 {
92
93
94 final GenericInterface<String> dependency1;
95
96
97 final GenericInterface<Double> dependency2;
98
99
100
101
102
103
104
105
106
107 SUT2(GenericInterface<String> dep1, GenericInterface<Double> dep2) {
108 dependency1 = dep1;
109 dependency2 = dep2;
110 }
111 }
112
113
114
115
116
117
118
119
120
121
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
132
133 static final class Dependency {
134 }
135
136
137
138
139 static final class Service1 extends GenericClass<Dependency> {
140 }
141
142
143
144
145 static final class Service2 {
146
147 Service1 service1;
148 }
149
150
151
152
153
154
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 }