1
2
3
4
5
6 package mockit;
7
8 import static org.junit.jupiter.api.Assertions.assertEquals;
9 import static org.junit.jupiter.api.Assertions.assertFalse;
10 import static org.junit.jupiter.api.Assertions.assertSame;
11 import static org.junit.jupiter.api.Assertions.assertTrue;
12
13 import mockit.integration.junit5.JMockitExtension;
14
15 import org.junit.jupiter.api.Test;
16 import org.junit.jupiter.api.extension.ExtendWith;
17
18
19
20
21 @ExtendWith(JMockitExtension.class)
22 class TestedAbstractClassTest {
23
24
25
26
27 public abstract static class AbstractClass implements Runnable {
28
29
30 private final int value;
31
32
33 protected String name;
34
35
36
37
38
39
40
41 protected AbstractClass(int value) {
42 this.value = value;
43 }
44
45
46
47
48
49
50 public final boolean doSomeOperation() {
51 run();
52 return doSomething() > 0;
53 }
54
55
56
57
58
59
60 protected abstract int doSomething();
61
62
63
64
65
66
67 public int getValue() {
68 return value;
69 }
70 }
71
72
73
74
75 @Tested
76 AbstractClass tested;
77
78
79 @Injectable("123")
80 int value;
81
82
83
84
85
86
87
88 @Test
89 void exerciseTestedObject(@Injectable("Test") String name) {
90 assertThatGeneratedSubclassIsAlwaysTheSame();
91 assertEquals(123, tested.getValue());
92 assertEquals("Test", tested.name);
93
94 new Expectations() {
95 {
96 tested.doSomething();
97 result = 23;
98 times = 1;
99 }
100 };
101
102 assertTrue(tested.doSomeOperation());
103
104 new Verifications() {
105 {
106 tested.run();
107 }
108 };
109 }
110
111
112
113
114 @Test
115 void exerciseDynamicallyMockedTestedObject() {
116 assertThatGeneratedSubclassIsAlwaysTheSame();
117 assertEquals(123, tested.getValue());
118
119 new Expectations(tested) {
120 {
121 tested.getValue();
122 result = 45;
123 tested.doSomething();
124 result = 7;
125 }
126 };
127
128 assertEquals(45, tested.getValue());
129 assertTrue(tested.doSomeOperation());
130
131 new Verifications() {
132 {
133 tested.run();
134 times = 1;
135 }
136 };
137 }
138
139
140
141
142
143
144
145 @Test
146 void exerciseTestedObjectAgain(@Injectable("Another test") String text) {
147 assertThatGeneratedSubclassIsAlwaysTheSame();
148 assertEquals(123, tested.getValue());
149 assertEquals("Another test", tested.name);
150
151 assertFalse(tested.doSomeOperation());
152
153 new VerificationsInOrder() {
154 {
155 tested.run();
156 tested.doSomething();
157 }
158 };
159 }
160
161
162 Class<?> generatedSubclass;
163
164
165
166
167 void assertThatGeneratedSubclassIsAlwaysTheSame() {
168 Class<?> testedClass = tested.getClass();
169
170 if (generatedSubclass == null) {
171 generatedSubclass = testedClass;
172 } else {
173 assertSame(generatedSubclass, testedClass);
174 }
175 }
176 }