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

hazendaz / jmockit1 / 496

15 Nov 2025 05:33PM UTC coverage: 72.192% (-0.008%) from 72.2%
496

push

github

web-flow
Merge pull request #412 from hazendaz/renovate/major-spring-core

Update spring core to v7 (major)

5677 of 8360 branches covered (67.91%)

Branch coverage included in aggregate %.

11922 of 16018 relevant lines covered (74.43%)

0.74 hits per line

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

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

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

11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.Map;
14

15
import mockit.internal.expectations.argumentMatching.ArgumentMatcher;
16
import mockit.internal.expectations.invocation.ExpectedInvocation;
17
import mockit.internal.expectations.invocation.InvocationArguments;
18

19
import org.checkerframework.checker.index.qual.NonNegative;
20

21
public abstract class BaseVerificationPhase extends TestOnlyPhase {
22
    @NonNull
23
    final ReplayPhase replayPhase;
24
    @NonNull
25
    private final List<VerifiedExpectation> currentVerifiedExpectations;
26
    @Nullable
27
    Expectation currentVerification;
28
    int replayIndex;
29
    @Nullable
30
    Error pendingError;
31
    @Nullable
32
    ExpectedInvocation matchingInvocationWithDifferentArgs;
33

34
    BaseVerificationPhase(@NonNull ReplayPhase replayPhase) {
35
        super(replayPhase.executionState);
1✔
36
        this.replayPhase = replayPhase;
1✔
37
        currentVerifiedExpectations = new ArrayList<>();
1✔
38
    }
1✔
39

40
    @Nullable
41
    @Override
42
    final Object handleInvocation(@Nullable Object mock, int mockAccess, @NonNull String mockClassDesc,
43
            @NonNull String mockNameAndDesc, @Nullable String genericSignature, boolean withRealImpl,
44
            @NonNull Object[] args) {
45
        if (pendingError != null) {
1✔
46
            replayPhase.failureState.setErrorThrown(pendingError);
1✔
47
            pendingError = null;
1✔
48
            return null;
1✔
49
        }
50

51
        matchInstance = mock != null && (executionState.equivalentInstances.isReplacementInstance(mock, mockNameAndDesc)
1✔
52
                || isEnumElement(mock));
1✔
53

54
        ExpectedInvocation currentInvocation = new ExpectedInvocation(mock, mockAccess, mockClassDesc, mockNameAndDesc,
1✔
55
                matchInstance, genericSignature, args);
56
        currentInvocation.arguments.setMatchers(argMatchers);
1✔
57
        currentVerification = new Expectation(currentInvocation);
1✔
58

59
        currentExpectation = null;
1✔
60
        currentVerifiedExpectations.clear();
1✔
61
        List<ExpectedInvocation> matchingInvocationsWithDifferentArgs = findExpectation(mock, mockClassDesc,
1✔
62
                mockNameAndDesc, args);
63
        argMatchers = null;
1✔
64

65
        if (replayPhase.failureState.getErrorThrown() != null) {
1!
66
            return null;
×
67
        }
68

69
        if (currentExpectation == null) {
1✔
70
            pendingError = currentVerification.invocation
1✔
71
                    .errorForMissingInvocation(matchingInvocationsWithDifferentArgs);
1✔
72
            currentExpectation = currentVerification;
1✔
73
        }
74

75
        return currentExpectation.invocation.getDefaultValueForReturnType();
1✔
76
    }
77

78
    @NonNull
79
    abstract List<ExpectedInvocation> findExpectation(@Nullable Object mock, @NonNull String mockClassDesc,
80
            @NonNull String mockNameAndDesc, @NonNull Object[] args);
81

82
    final boolean matches(@Nullable Object mock, @NonNull String mockClassDesc, @NonNull String mockNameAndDesc,
83
            @NonNull Object[] args, @NonNull Expectation replayExpectation, @Nullable Object replayInstance,
84
            @NonNull Object[] replayArgs) {
85
        ExpectedInvocation invocation = replayExpectation.invocation;
1✔
86
        boolean constructor = invocation.isConstructor();
1✔
87
        Map<Object, Object> replacementMap = getReplacementMap();
1✔
88
        matchingInvocationWithDifferentArgs = null;
1✔
89

90
        if (invocation.isMatch(mock, mockClassDesc, mockNameAndDesc, replacementMap)) {
1✔
91
            boolean matching;
92

93
            if (mock == null || invocation.instance == null || constructor && !matchInstance) {
1!
94
                matching = true;
1✔
95
            } else {
96
                matching = executionState.equivalentInstances.areMatchingInstances(matchInstance, invocation.instance,
1✔
97
                        mock);
98
            }
99

100
            if (matching) {
1✔
101
                matchingInvocationWithDifferentArgs = invocation;
1✔
102

103
                InvocationArguments invocationArguments = invocation.arguments;
1✔
104
                List<ArgumentMatcher<?>> originalMatchers = invocationArguments.getMatchers();
1✔
105
                Object[] originalArgs = invocationArguments.prepareForVerification(args, argMatchers);
1✔
106
                boolean argumentsMatch = invocationArguments.isMatch(replayArgs, getInstanceMap());
1✔
107
                invocationArguments.setValuesAndMatchers(originalArgs, originalMatchers);
1✔
108

109
                if (argumentsMatch) {
1✔
110
                    addVerifiedExpectation(replayExpectation, replayArgs);
1✔
111
                    return true;
1✔
112
                }
113
            }
114
        }
115

116
        return false;
1✔
117
    }
118

119
    abstract void addVerifiedExpectation(@NonNull Expectation expectation, @NonNull Object[] args);
120

121
    final void addVerifiedExpectation(@NonNull VerifiedExpectation verifiedExpectation) {
122
        executionState.verifiedExpectations.add(verifiedExpectation);
1✔
123
        currentVerifiedExpectations.add(verifiedExpectation);
1✔
124
    }
1✔
125

126
    @Override
127
    final void setMaxInvocationCount(int maxInvocations) {
128
        if (maxInvocations == 0 || pendingError == null) {
1!
129
            super.setMaxInvocationCount(maxInvocations);
1✔
130
        }
131
    }
1✔
132

133
    @Nullable
134
    Error endVerification() {
135
        return pendingError;
1✔
136
    }
137

138
    @Nullable
139
    final Object getArgumentValueForCurrentVerification(@NonNegative int parameterIndex) {
140
        List<VerifiedExpectation> verifiedExpectations = executionState.verifiedExpectations;
1✔
141

142
        if (verifiedExpectations.isEmpty()) {
1✔
143
            return currentVerification == null ? null
1!
144
                    : currentVerification.invocation.getArgumentValues()[parameterIndex];
1✔
145
        }
146

147
        VerifiedExpectation lastMatched = verifiedExpectations.get(verifiedExpectations.size() - 1);
1✔
148
        return lastMatched.arguments[parameterIndex];
1✔
149
    }
150

151
    @NonNull
152
    public final <T> List<T> getNewInstancesMatchingVerifiedConstructorInvocation() {
153
        List<T> newInstances = new ArrayList<>();
1✔
154

155
        for (VerifiedExpectation verifiedExpectation : currentVerifiedExpectations) {
1✔
156
            // noinspection unchecked
157
            T newInstance = (T) verifiedExpectation.captureNewInstance();
1✔
158
            newInstances.add(newInstance);
1✔
159
        }
1✔
160

161
        return newInstances;
1✔
162
    }
163
}
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