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

hazendaz / jmockit1 / 469

08 Nov 2025 03:55AM UTC coverage: 72.268% (+0.005%) from 72.263%
469

push

github

web-flow
Merge pull request #404 from hazendaz/renovate/hibernate-orm-monorepo

Update dependency org.hibernate.orm:hibernate-core to v7.1.7.Final

5677 of 8360 branches covered (67.91%)

Branch coverage included in aggregate %.

11939 of 16016 relevant lines covered (74.54%)

0.75 hits per line

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

57.69
/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
            return;
×
60
        }
61

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

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

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

83
        if (beforeAllMethod != null) {
×
84
            initContext = new ParamValueInitContext(testInstance, testClass, beforeAllMethod, null);
×
85
            parameterValues = createInstancesForAnnotatedParameters(testInstance, beforeAllMethod, null);
×
86
        }
87
    }
×
88

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

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

100
        TestRun.enterNoMockingZone();
1✔
101

102
        try {
103
            handleMockFieldsForWholeTestClass(testInstance);
1✔
104
        } finally {
105
            TestRun.exitNoMockingZone();
1✔
106
        }
107

108
        TestRun.setRunningIndividualTest(testInstance);
1✔
109
    }
1✔
110

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

121
        TestRun.prepareForNextTest();
1✔
122
        TestRun.enterNoMockingZone();
1✔
123

124
        try {
125
            savePointForTest = new SavePoint();
1✔
126
            createInstancesForTestedFieldsBeforeSetup(testInstance);
1✔
127

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

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

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

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

156
        TestRun.enterNoMockingZone();
1✔
157

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

168
        TestRun.setRunningIndividualTest(testInstance);
1✔
169
    }
1✔
170

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

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

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

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

209
        TestRun.enterNoMockingZone();
1✔
210

211
        try {
212
            savePointForTestMethod.rollback();
1✔
213
            savePointForTestMethod = null;
1✔
214

215
            if (thrownByTest != null) {
1!
216
                filterStackTrace(thrownByTest);
×
217
            }
218

219
            Error expectationsFailure = RecordAndReplayExecution.endCurrentReplayIfAny();
1✔
220
            clearTestedObjectsIfAny();
1✔
221

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

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

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

246
            clearFieldTypeRedefinitions();
1✔
247
            TestRun.setCurrentTestClass(null);
1✔
248
        }
249
    }
1✔
250

251
    private static class ParamValueInitContext {
252
        private final Object instance;
253
        private final Class<?> clazz;
254
        private final Method method;
255
        private final String warning;
256

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

264
        boolean isBeforeAllMethod() {
265
            return method != null && method.getDeclaredAnnotation(BeforeAll.class) != null;
×
266
        }
267

268
        boolean isBeforeEachMethod() {
269
            return method != null && method.getDeclaredAnnotation(BeforeEach.class) != null;
×
270
        }
271

272
        String displayClass() {
273
            return clazz == null ? "<no class reference>" : clazz.getName();
×
274
        }
275

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

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