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

hazendaz / jmockit1 / 546

28 Nov 2025 01:17AM UTC coverage: 72.2%. Remained the same
546

push

github

web-flow
Update JDK requirement and module details in README

Updated JDK version requirement and modified module descriptions.

5679 of 8362 branches covered (67.91%)

Branch coverage included in aggregate %.

11924 of 16019 relevant lines covered (74.44%)

0.74 hits per line

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

57.84
/main/src/main/java/mockit/integration/junit5/JMockitExtension.java
1
/*
2
 * MIT License
3
 * Copyright (c) 2006-2025 JMockit developers
4
 * See LICENSE file for full license text.
5
 */
6
package mockit.integration.junit5;
7

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

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

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

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

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

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

57
    @Override
58
    public void beforeAll(@NonNull ExtensionContext context) {
59
        if (!isRegularTestClass(context)) {
1!
60
            return;
×
61
        }
62

63
        @Nullable
64
        Class<?> testClass = context.getTestClass().orElse(null);
1✔
65
        savePointForTestClass = new SavePoint();
1✔
66
        // Ensure JMockit state and test class logic is handled before any test instance is created
67
        if (testClass != null) {
1!
68
            updateTestClassState(null, testClass);
1✔
69
        }
70

71
        if (testClass == null) {
1!
72
            initContext = new ParamValueInitContext(null, null, null,
×
73
                    "@BeforeAll setup failed to acquire 'Class' of test");
74
            return;
×
75
        }
76

77
        // @BeforeAll can be used on instance methods depending on @TestInstance(PER_CLASS) usage
78
        Object testInstance = context.getTestInstance().orElse(null);
1✔
79
        Method beforeAllMethod = Utilities.getAnnotatedDeclaredMethod(testClass, BeforeAll.class);
1✔
80
        if (testInstance == null) {
1!
81
            initContext = new ParamValueInitContext(null, testClass, beforeAllMethod,
1✔
82
                    "@BeforeAll setup failed to acquire instance of test class");
83
            return;
1✔
84
        }
85

86
        if (beforeAllMethod != null) {
×
87
            initContext = new ParamValueInitContext(testInstance, testClass, beforeAllMethod, null);
×
88
            parameterValues = createInstancesForAnnotatedParameters(testInstance, beforeAllMethod, null);
×
89
        }
90
    }
×
91

92
    private static boolean isRegularTestClass(@NonNull ExtensionContext context) {
93
        Class<?> testClass = context.getTestClass().orElse(null);
1✔
94
        return testClass != null && !testClass.isAnnotationPresent(Nested.class);
1!
95
    }
96

97
    @Override
98
    public void postProcessTestInstance(@NonNull Object testInstance, @NonNull ExtensionContext context) {
99
        if (!isRegularTestClass(context)) {
1!
100
            return;
×
101
        }
102

103
        TestRun.enterNoMockingZone();
1✔
104

105
        try {
106
            handleMockFieldsForWholeTestClass(testInstance);
1✔
107
        } finally {
108
            TestRun.exitNoMockingZone();
1✔
109
        }
110

111
        TestRun.setRunningIndividualTest(testInstance);
1✔
112
    }
1✔
113

114
    @Override
115
    public void beforeEach(@NonNull ExtensionContext context) {
116
        Object testInstance = context.getTestInstance().orElse(null);
1✔
117
        Class<?> testClass = context.getTestClass().orElse(null);
1✔
118
        if (testInstance == null) {
1!
119
            initContext = new ParamValueInitContext(null, null, null,
×
120
                    "@BeforeEach setup failed to acquire instance of test class");
121
            return;
×
122
        }
123

124
        TestRun.prepareForNextTest();
1✔
125
        TestRun.enterNoMockingZone();
1✔
126

127
        try {
128
            savePointForTest = new SavePoint();
1✔
129
            createInstancesForTestedFieldsBeforeSetup(testInstance);
1✔
130

131
            if (testClass == null) {
1!
132
                initContext = new ParamValueInitContext(null, null, null,
×
133
                        "@BeforeEach setup failed to acquire Class<?> of test");
134
                return;
×
135
            }
136

137
            Method beforeEachMethod = Utilities.getAnnotatedDeclaredMethod(testClass, BeforeEach.class);
1✔
138
            if (beforeEachMethod != null) {
1✔
139
                initContext = new ParamValueInitContext(testInstance, testClass, beforeEachMethod, null);
1✔
140
                parameterValues = createInstancesForAnnotatedParameters(testInstance, beforeEachMethod, null);
1✔
141
            }
142
        } finally {
143
            TestRun.exitNoMockingZone();
1✔
144
        }
145
    }
1✔
146

147
    @Override
148
    public void beforeTestExecution(@NonNull ExtensionContext context) {
149
        Class<?> testClass = context.getTestClass().orElse(null);
1✔
150
        Method testMethod = context.getTestMethod().orElse(null);
1✔
151
        Object testInstance = context.getTestInstance().orElse(null);
1✔
152

153
        if (testMethod == null || testInstance == null) {
1!
154
            initContext = new ParamValueInitContext(testInstance, testClass, testMethod,
×
155
                    "@Test failed to acquire instance of test class, or target method");
156
            return;
×
157
        }
158

159
        TestRun.enterNoMockingZone();
1✔
160

161
        try {
162
            savePointForTestMethod = new SavePoint();
1✔
163
            createInstancesForTestedFieldsFromBaseClasses(testInstance);
1✔
164
            initContext = new ParamValueInitContext(testInstance, testClass, testMethod, null);
1✔
165
            parameterValues = createInstancesForAnnotatedParameters(testInstance, testMethod, null);
1✔
166
            createInstancesForTestedFields(testInstance);
1✔
167
        } finally {
168
            TestRun.exitNoMockingZone();
1✔
169
        }
170

171
        TestRun.setRunningIndividualTest(testInstance);
1✔
172
    }
1✔
173

174
    @Override
175
    public boolean supportsParameter(@NonNull ParameterContext parameterContext,
176
            @NonNull ExtensionContext extensionContext) {
177
        return parameterContext.isAnnotated(Tested.class) || parameterContext.isAnnotated(Mocked.class)
1✔
178
                || parameterContext.isAnnotated(Injectable.class) || parameterContext.isAnnotated(Capturing.class);
1!
179
    }
180

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

199
    @Override
200
    public void handleTestExecutionException(@NonNull ExtensionContext context, @NonNull Throwable throwable)
201
            throws Throwable {
202
        thrownByTest = throwable;
×
203
        throw throwable;
×
204
    }
205

206
    @Override
207
    public void afterTestExecution(@NonNull ExtensionContext context) {
208
        if (savePointForTestMethod == null) {
1!
209
            return;
×
210
        }
211

212
        TestRun.enterNoMockingZone();
1✔
213

214
        try {
215
            savePointForTestMethod.rollback();
1✔
216
            savePointForTestMethod = null;
1✔
217

218
            if (thrownByTest != null) {
1!
219
                filterStackTrace(thrownByTest);
×
220
            }
221

222
            Error expectationsFailure = RecordAndReplayExecution.endCurrentReplayIfAny();
1✔
223
            clearTestedObjectsIfAny();
1✔
224

225
            if (expectationsFailure != null) {
1!
226
                filterStackTrace(expectationsFailure);
×
227
                throw expectationsFailure;
×
228
            }
229
        } finally {
230
            TestRun.finishCurrentTestExecution();
1✔
231
            TestRun.exitNoMockingZone();
1✔
232
        }
233
    }
1✔
234

235
    @Override
236
    public void afterEach(@NonNull ExtensionContext context) {
237
        if (savePointForTest != null) {
1!
238
            savePointForTest.rollback();
1✔
239
            savePointForTest = null;
1✔
240
        }
241
    }
1✔
242

243
    @Override
244
    public void afterAll(@NonNull ExtensionContext context) {
245
        if (savePointForTestClass != null && isRegularTestClass(context)) {
1!
246
            savePointForTestClass.rollback();
1✔
247
            savePointForTestClass = null;
1✔
248

249
            clearFieldTypeRedefinitions();
1✔
250
            TestRun.setCurrentTestClass(null);
1✔
251
        }
252
    }
1✔
253

254
    private static class ParamValueInitContext {
255
        private final Object instance;
256
        private final Class<?> clazz;
257
        private final Method method;
258
        private final String warning;
259

260
        ParamValueInitContext(Object instance, Class<?> clazz, Method method, String warning) {
1✔
261
            this.instance = instance;
1✔
262
            this.clazz = clazz;
1✔
263
            this.method = method;
1✔
264
            this.warning = warning;
1✔
265
        }
1✔
266

267
        boolean isBeforeAllMethod() {
268
            return method != null && method.getDeclaredAnnotation(BeforeAll.class) != null;
×
269
        }
270

271
        boolean isBeforeEachMethod() {
272
            return method != null && method.getDeclaredAnnotation(BeforeEach.class) != null;
×
273
        }
274

275
        String displayClass() {
276
            return clazz == null ? "<no class reference>" : clazz.getName();
×
277
        }
278

279
        String displayMethod() {
280
            if (method == null) {
×
281
                return "<no method reference>";
×
282
            }
283
            String methodPrefix = isBeforeAllMethod() ? "@BeforeAll " : isBeforeEachMethod() ? "@BeforeEach " : "";
×
284
            String args = Arrays.stream(method.getParameterTypes()).map(Class::getName)
×
285
                    .collect(Collectors.joining(", "));
×
286
            return methodPrefix + method.getName() + "(" + args + ")";
×
287
        }
288

289
        @Override
290
        public String toString() {
291
            return "ParamContext{hasInstance=" + (instance == null ? "false" : "true") + ", class=" + clazz
×
292
                    + ", method=" + method + ", warning=" + warning + "}";
293
        }
294
    }
295
}
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