• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / jmockit1 / 421

30 Oct 2025 07:01PM UTC coverage: 72.198% (+0.02%) from 72.177%
421

push

github

hazendaz
Use better logic for remove redundant source directories

5681 of 8356 branches covered (67.99%)

Branch coverage included in aggregate %.

0 of 6 new or added lines in 1 file covered. (0.0%)

36 existing lines in 4 files now uncovered.

11941 of 16052 relevant lines covered (74.39%)

0.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.32
/main/src/main/java/mockit/integration/junit5/JMockitExtension.java
1
/*
2
 * Copyright (c) 2006 JMockit developers
3
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
4
 */
5
package mockit.integration.junit5;
6

7
import static mockit.internal.util.StackTrace.filterStackTrace;
8

9
import edu.umd.cs.findbugs.annotations.NonNull;
10
import edu.umd.cs.findbugs.annotations.Nullable;
11

12
import java.lang.reflect.Method;
13
import java.util.Arrays;
14
import java.util.stream.Collectors;
15

16
import mockit.Capturing;
17
import mockit.Injectable;
18
import mockit.Mocked;
19
import mockit.Tested;
20
import mockit.integration.TestRunnerDecorator;
21
import mockit.internal.expectations.RecordAndReplayExecution;
22
import mockit.internal.state.SavePoint;
23
import mockit.internal.state.TestRun;
24
import mockit.internal.util.Utilities;
25

26
import org.junit.jupiter.api.BeforeAll;
27
import org.junit.jupiter.api.BeforeEach;
28
import org.junit.jupiter.api.Nested;
29
import org.junit.jupiter.api.extension.AfterAllCallback;
30
import org.junit.jupiter.api.extension.AfterEachCallback;
31
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
32
import org.junit.jupiter.api.extension.BeforeAllCallback;
33
import org.junit.jupiter.api.extension.BeforeEachCallback;
34
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
35
import org.junit.jupiter.api.extension.ExtensionContext;
36
import org.junit.jupiter.api.extension.ParameterContext;
37
import org.junit.jupiter.api.extension.ParameterResolver;
38
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
39
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
40

41
public final class JMockitExtension extends TestRunnerDecorator implements BeforeAllCallback, AfterAllCallback,
1✔
42
        TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, BeforeTestExecutionCallback,
43
        AfterTestExecutionCallback, ParameterResolver, TestExecutionExceptionHandler {
44
    @Nullable
45
    private SavePoint savePointForTestClass;
46
    @Nullable
47
    private SavePoint savePointForTest;
48
    @Nullable
49
    private SavePoint savePointForTestMethod;
50
    @Nullable
51
    private Throwable thrownByTest;
52
    private Object[] parameterValues;
53
    private ParamValueInitContext initContext = new ParamValueInitContext(null, null, null,
1✔
54
            "No callbacks have been processed, preventing parameter population");
55

56
    @Override
57
    public void beforeAll(@NonNull ExtensionContext context) {
58
        if (isRegularTestClass(context)) {
1!
59
            @Nullable
60
            Class<?> testClass = context.getTestClass().orElse(null);
1✔
61
            savePointForTestClass = new SavePoint();
1✔
62
            TestRun.setCurrentTestClass(testClass);
1✔
63

64
            if (testClass == null) {
1!
UNCOV
65
                initContext = new ParamValueInitContext(null, null, null,
×
66
                        "@BeforeAll setup failed to acquire 'Class' of test");
UNCOV
67
                return;
×
68
            }
69

70
            // @BeforeAll can be used on instance methods depending on @TestInstance(PER_CLASS) usage
71
            Object testInstance = context.getTestInstance().orElse(null);
1✔
72
            Method beforeAllMethod = Utilities.getAnnotatedDeclaredMethod(testClass, BeforeAll.class);
1✔
73
            if (testInstance == null) {
1!
74
                initContext = new ParamValueInitContext(null, testClass, beforeAllMethod,
1✔
75
                        "@BeforeAll setup failed to acquire instance of test class");
76
                return;
1✔
77
            }
78

UNCOV
79
            if (beforeAllMethod != null) {
×
80
                initContext = new ParamValueInitContext(testInstance, testClass, beforeAllMethod, null);
×
81
                parameterValues = createInstancesForAnnotatedParameters(testInstance, beforeAllMethod, null);
×
82
            }
83
        }
UNCOV
84
    }
×
85

86
    private static boolean isRegularTestClass(@NonNull ExtensionContext context) {
87
        Class<?> testClass = context.getTestClass().orElse(null);
1✔
88
        return testClass != null && !testClass.isAnnotationPresent(Nested.class);
1!
89
    }
90

91
    @Override
92
    public void postProcessTestInstance(@NonNull Object testInstance, @NonNull ExtensionContext context) {
93
        if (isRegularTestClass(context)) {
1!
94
            TestRun.enterNoMockingZone();
1✔
95

96
            try {
97
                handleMockFieldsForWholeTestClass(testInstance);
1✔
98
            } finally {
99
                TestRun.exitNoMockingZone();
1✔
100
            }
101

102
            TestRun.setRunningIndividualTest(testInstance);
1✔
103
        }
104
    }
1✔
105

106
    @Override
107
    public void beforeEach(@NonNull ExtensionContext context) {
108
        Object testInstance = context.getTestInstance().orElse(null);
1✔
109
        Class<?> testClass = context.getTestClass().orElse(null);
1✔
110
        if (testInstance == null) {
1!
UNCOV
111
            initContext = new ParamValueInitContext(null, null, null,
×
112
                    "@BeforeEach setup failed to acquire instance of test class");
UNCOV
113
            return;
×
114
        }
115

116
        TestRun.prepareForNextTest();
1✔
117
        TestRun.enterNoMockingZone();
1✔
118

119
        try {
120
            savePointForTest = new SavePoint();
1✔
121
            createInstancesForTestedFieldsBeforeSetup(testInstance);
1✔
122

123
            if (testClass == null) {
1!
UNCOV
124
                initContext = new ParamValueInitContext(null, null, null,
×
125
                        "@BeforeEach setup failed to acquire Class<?> of test");
UNCOV
126
                return;
×
127
            }
128

129
            Method beforeEachMethod = Utilities.getAnnotatedDeclaredMethod(testClass, BeforeEach.class);
1✔
130
            if (beforeEachMethod != null) {
1✔
131
                initContext = new ParamValueInitContext(testInstance, testClass, beforeEachMethod, null);
1✔
132
                parameterValues = createInstancesForAnnotatedParameters(testInstance, beforeEachMethod, null);
1✔
133
            }
134
        } finally {
135
            TestRun.exitNoMockingZone();
1✔
136
        }
137
    }
1✔
138

139
    @Override
140
    public void beforeTestExecution(@NonNull ExtensionContext context) {
141
        Class<?> testClass = context.getTestClass().orElse(null);
1✔
142
        Method testMethod = context.getTestMethod().orElse(null);
1✔
143
        Object testInstance = context.getTestInstance().orElse(null);
1✔
144

145
        if (testMethod == null || testInstance == null) {
1!
UNCOV
146
            initContext = new ParamValueInitContext(testInstance, testClass, testMethod,
×
147
                    "@Test failed to acquire instance of test class, or target method");
UNCOV
148
            return;
×
149
        }
150

151
        TestRun.enterNoMockingZone();
1✔
152

153
        try {
154
            savePointForTestMethod = new SavePoint();
1✔
155
            createInstancesForTestedFieldsFromBaseClasses(testInstance);
1✔
156
            initContext = new ParamValueInitContext(testInstance, testClass, testMethod, null);
1✔
157
            parameterValues = createInstancesForAnnotatedParameters(testInstance, testMethod, null);
1✔
158
            createInstancesForTestedFields(testInstance);
1✔
159
        } finally {
160
            TestRun.exitNoMockingZone();
1✔
161
        }
162

163
        TestRun.setRunningIndividualTest(testInstance);
1✔
164
    }
1✔
165

166
    @Override
167
    public boolean supportsParameter(@NonNull ParameterContext parameterContext,
168
            @NonNull ExtensionContext extensionContext) {
169
        return parameterContext.isAnnotated(Tested.class) || parameterContext.isAnnotated(Mocked.class)
1✔
170
                || parameterContext.isAnnotated(Injectable.class) || parameterContext.isAnnotated(Capturing.class);
1!
171
    }
172

173
    @Override
174
    public Object resolveParameter(@NonNull ParameterContext parameterContext,
175
            @NonNull ExtensionContext extensionContext) {
176
        int parameterIndex = parameterContext.getIndex();
1✔
177
        if (parameterValues == null) {
1!
UNCOV
178
            String warning = initContext.warning;
×
179
            StringBuilder exceptionMessage = new StringBuilder(
×
180
                    "JMockit failed to provide parameters to JUnit 5 ParameterResolver.");
UNCOV
181
            if (warning != null) {
×
182
                exceptionMessage.append("\nAdditional info: ").append(warning);
×
183
            }
UNCOV
184
            exceptionMessage.append("\n - Class: ").append(initContext.displayClass());
×
185
            exceptionMessage.append("\n - Method: ").append(initContext.displayMethod());
×
186
            throw new IllegalStateException(exceptionMessage.toString());
×
187
        }
188
        return parameterValues[parameterIndex];
1✔
189
    }
190

191
    @Override
192
    public void handleTestExecutionException(@NonNull ExtensionContext context, @NonNull Throwable throwable)
193
            throws Throwable {
UNCOV
194
        thrownByTest = throwable;
×
195
        throw throwable;
×
196
    }
197

198
    @Override
199
    public void afterTestExecution(@NonNull ExtensionContext context) {
200
        if (savePointForTestMethod != null) {
1!
201
            TestRun.enterNoMockingZone();
1✔
202

203
            try {
204
                savePointForTestMethod.rollback();
1✔
205
                savePointForTestMethod = null;
1✔
206

207
                if (thrownByTest != null) {
1!
UNCOV
208
                    filterStackTrace(thrownByTest);
×
209
                }
210

211
                Error expectationsFailure = RecordAndReplayExecution.endCurrentReplayIfAny();
1✔
212
                clearTestedObjectsIfAny();
1✔
213

214
                if (expectationsFailure != null) {
1!
UNCOV
215
                    filterStackTrace(expectationsFailure);
×
216
                    throw expectationsFailure;
×
217
                }
218
            } finally {
219
                TestRun.finishCurrentTestExecution();
1✔
220
                TestRun.exitNoMockingZone();
1✔
221
            }
222
        }
223
    }
1✔
224

225
    @Override
226
    public void afterEach(@NonNull ExtensionContext context) {
227
        if (savePointForTest != null) {
1!
228
            savePointForTest.rollback();
1✔
229
            savePointForTest = null;
1✔
230
        }
231
    }
1✔
232

233
    @Override
234
    public void afterAll(@NonNull ExtensionContext context) {
235
        if (savePointForTestClass != null && isRegularTestClass(context)) {
1!
236
            savePointForTestClass.rollback();
1✔
237
            savePointForTestClass = null;
1✔
238

239
            clearFieldTypeRedefinitions();
1✔
240
            TestRun.setCurrentTestClass(null);
1✔
241
        }
242
    }
1✔
243

244
    private static class ParamValueInitContext {
245
        private final Object instance;
246
        private final Class<?> clazz;
247
        private final Method method;
248
        private final String warning;
249

250
        ParamValueInitContext(Object instance, Class<?> clazz, Method method, String warning) {
1✔
251
            this.instance = instance;
1✔
252
            this.clazz = clazz;
1✔
253
            this.method = method;
1✔
254
            this.warning = warning;
1✔
255
        }
1✔
256

257
        boolean isBeforeAllMethod() {
UNCOV
258
            return method.getDeclaredAnnotation(BeforeAll.class) != null;
×
259
        }
260

261
        boolean isBeforeEachMethod() {
UNCOV
262
            return method.getDeclaredAnnotation(BeforeEach.class) != null;
×
263
        }
264

265
        String displayClass() {
UNCOV
266
            if (clazz == null) {
×
267
                return "<no class reference>";
×
268
            }
UNCOV
269
            return clazz.getName();
×
270
        }
271

272
        String displayMethod() {
UNCOV
273
            if (method == null) {
×
274
                return "<no method reference>";
×
275
            }
UNCOV
276
            String methodPrefix = isBeforeAllMethod() ? "@BeforeAll " : isBeforeEachMethod() ? "@BeforeEach " : "";
×
277
            String args = Arrays.stream(method.getParameterTypes()).map(Class::getName)
×
278
                    .collect(Collectors.joining(", "));
×
279
            return methodPrefix + method.getName() + "(" + args + ")";
×
280
        }
281

282
        @Override
283
        public String toString() {
UNCOV
284
            return "ParamContext{" + "hasInstance=" + (instance == null ? "false" : "true") + ", class=" + clazz
×
285
                    + ", method=" + method + '}';
286
        }
287
    }
288
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc