1
2
3
4
5
6 package mockit;
7
8 import static org.junit.jupiter.api.Assertions.assertEquals;
9
10 import mockit.integration.junit5.JMockitExtension;
11
12 import org.junit.jupiter.api.Test;
13 import org.junit.jupiter.api.extension.ExtendWith;
14
15
16
17
18 @ExtendWith(JMockitExtension.class)
19 class TestedClassWithFieldDIByTypeAndNameTest {
20
21
22
23
24 static class TestedClass {
25
26
27 int someValue;
28
29
30 int anotherValue;
31
32
33
34
35
36
37 final int getSomeValue_base() {
38 return someValue;
39 }
40 }
41
42
43
44
45 static class TestedSubclass extends TestedClass {
46
47
48 @SuppressWarnings("FieldNameHidesFieldInSuperclass")
49 int someValue;
50
51
52 int yetAnotherValue;
53 }
54
55
56 @Tested
57 TestedSubclass tested;
58
59
60
61
62
63
64
65 @Test
66 void injectByFieldTypeAndNameWithTestedClassHavingMultipleFieldsOfSameType(@Injectable("12") int anotherValue) {
67 assertEquals(0, tested.getSomeValue_base());
68 assertEquals(0, tested.someValue);
69 assertEquals(12, tested.anotherValue);
70 assertEquals(0, tested.yetAnotherValue);
71 }
72
73
74
75
76
77
78
79 @Test
80 void injectByFieldTypeAndNameWithTestedClassHavingFieldsOfSameTypeButDifferentNames(@Injectable("45") int val) {
81 assertEquals(0, tested.getSomeValue_base());
82 assertEquals(0, tested.someValue);
83 assertEquals(0, tested.anotherValue);
84 assertEquals(0, tested.yetAnotherValue);
85 }
86
87
88
89
90
91
92
93
94
95
96
97 @Test
98 void injectByFieldTypeAndNameIntoFieldsAtDifferentLevelsOfClassHierarchy(@Injectable("1") int someValue,
99 @Injectable("2") int yetAnotherValue, @Injectable("3") int unused) {
100 assertEquals(0, tested.getSomeValue_base());
101 assertEquals(1, tested.someValue);
102 assertEquals(0, tested.anotherValue);
103 assertEquals(2, tested.yetAnotherValue);
104 }
105 }