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