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

hazendaz / jmockit1 / 547

28 Nov 2025 01:29AM UTC coverage: 72.203% (+0.003%) from 72.2%
547

Pull #424

github

web-flow
Merge 949a2e7c0 into ec1f220fb
Pull Request #424: Expected exception annotation

5692 of 8382 branches covered (67.91%)

Branch coverage included in aggregate %.

11 of 14 new or added lines in 1 file covered. (78.57%)

23 existing lines in 1 file now uncovered.

11935 of 16031 relevant lines covered (74.45%)

0.74 hits per line

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

60.37
/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 edu.umd.cs.findbugs.annotations.NonNull;
9
import edu.umd.cs.findbugs.annotations.Nullable;
10

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

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

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

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

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

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

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

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

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

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

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

104
        TestRun.enterNoMockingZone();
1✔
105

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

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

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

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

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

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

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

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

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

160
        TestRun.enterNoMockingZone();
1✔
161

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

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

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

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

200
    @Override
201
    public void handleTestExecutionException(@NonNull ExtensionContext context, @NonNull Throwable throwable)
202
            throws Throwable {
203
        if (isExpectedException(context, throwable)) {
1!
204
            // Expected JMockit error was thrown, suppress it (test passes)
205
            return;
1✔
206
        }
207

208
        thrownByTest = throwable;
×
209
        throw throwable;
×
210
    }
211

212
    @Override
213
    public void afterTestExecution(@NonNull ExtensionContext context) {
214
        if (savePointForTestMethod == null) {
1!
UNCOV
215
            return;
×
216
        }
217

218
        TestRun.enterNoMockingZone();
1✔
219

220
        try {
221
            savePointForTestMethod.rollback();
1✔
222
            savePointForTestMethod = null;
1✔
223

224
            if (thrownByTest != null) {
1!
NEW
UNCOV
225
                StackTrace.filterStackTrace(thrownByTest);
×
226
            }
227

228
            Error expectationsFailure = RecordAndReplayExecution.endCurrentReplayIfAny();
1✔
229
            clearTestedObjectsIfAny();
1✔
230

231
            if (expectationsFailure != null && isExpectedException(context, expectationsFailure)) {
1!
232
                // Expected JMockit error was thrown, suppress it (test passes)
233
                return;
1✔
234
            }
235

236
            if (expectationsFailure != null) {
1!
NEW
237
                StackTrace.filterStackTrace(expectationsFailure);
×
238
                throw expectationsFailure;
×
239
            }
240
        } finally {
241
            TestRun.finishCurrentTestExecution();
1✔
242
            TestRun.exitNoMockingZone();
1✔
243
        }
244
    }
1✔
245

246
    @Override
247
    public void afterEach(@NonNull ExtensionContext context) {
248
        if (savePointForTest != null) {
1!
249
            savePointForTest.rollback();
1✔
250
            savePointForTest = null;
1✔
251
        }
252
    }
1✔
253

254
    @Override
255
    public void afterAll(@NonNull ExtensionContext context) {
256
        if (savePointForTestClass != null && isRegularTestClass(context)) {
1!
257
            savePointForTestClass.rollback();
1✔
258
            savePointForTestClass = null;
1✔
259

260
            clearFieldTypeRedefinitions();
1✔
261
            TestRun.setCurrentTestClass(null);
1✔
262
        }
263
    }
1✔
264

265
    private static class ParamValueInitContext {
266
        private final Object instance;
267
        private final Class<?> clazz;
268
        private final Method method;
269
        private final String warning;
270

271
        ParamValueInitContext(Object instance, Class<?> clazz, Method method, String warning) {
1✔
272
            this.instance = instance;
1✔
273
            this.clazz = clazz;
1✔
274
            this.method = method;
1✔
275
            this.warning = warning;
1✔
276
        }
1✔
277

278
        boolean isBeforeAllMethod() {
UNCOV
279
            return method != null && method.getDeclaredAnnotation(BeforeAll.class) != null;
×
280
        }
281

282
        boolean isBeforeEachMethod() {
283
            return method != null && method.getDeclaredAnnotation(BeforeEach.class) != null;
×
284
        }
285

286
        String displayClass() {
287
            return clazz == null ? "<no class reference>" : clazz.getName();
×
288
        }
289

290
        String displayMethod() {
291
            if (method == null) {
×
UNCOV
292
                return "<no method reference>";
×
293
            }
UNCOV
294
            String methodPrefix = isBeforeAllMethod() ? "@BeforeAll " : isBeforeEachMethod() ? "@BeforeEach " : "";
×
295
            String args = Arrays.stream(method.getParameterTypes()).map(Class::getName)
×
296
                    .collect(Collectors.joining(", "));
×
UNCOV
297
            return methodPrefix + method.getName() + "(" + args + ")";
×
298
        }
299

300
        @Override
301
        public String toString() {
UNCOV
302
            return "ParamContext{hasInstance=" + (instance == null ? "false" : "true") + ", class=" + clazz
×
303
                    + ", method=" + method + ", warning=" + warning + "}";
304
        }
305
    }
306

307
    private static boolean isExpectedException(@NonNull ExtensionContext context, @NonNull Throwable throwable) {
308
        Method testMethod = context.getTestMethod().orElse(null);
1✔
309
        ExpectedException expectedException = testMethod != null ? testMethod.getAnnotation(ExpectedException.class)
1!
310
                : null;
1✔
311

312
        if (expectedException != null && isUnexpectedOrMissingInvocation(throwable)) {
1!
313
            return expectedException.deferred() || expectedException.value().isInstance(throwable);
1!
314
        }
NEW
315
        return false;
×
316
    }
317

318
    private static boolean isUnexpectedOrMissingInvocation(@NonNull Throwable error) {
319
        Class<?> errorType = error.getClass();
1✔
320
        return errorType == UnexpectedInvocation.class || errorType == MissingInvocation.class;
1!
321
    }
322

323
}
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