All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.internal.expectations.InstanceBasedMatching Maven / Gradle / Ivy

Go to download

JMockit is a Java toolkit for automated developer testing. It contains APIs for the creation of the objects to be tested, for mocking dependencies, and for faking external APIs; JUnit (4 & 5) and TestNG test runners are supported. It also contains an advanced code coverage tool.

The newest version!
/*
 * Copyright (c) 2006 JMockit developers
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.expectations;

import static mockit.internal.util.Utilities.containsReference;

import edu.umd.cs.findbugs.annotations.NonNull;

import java.util.LinkedList;
import java.util.List;

import mockit.internal.util.GeneratedClasses;

final class InstanceBasedMatching {
    @NonNull
    private final List> mockedTypesToMatchOnInstances;

    InstanceBasedMatching() {
        mockedTypesToMatchOnInstances = new LinkedList<>();
    }

    void discoverMockedTypesToMatchOnInstances(@NonNull List> targetClasses) {
        int numClasses = targetClasses.size();

        if (numClasses > 1) {
            for (int i = 0; i < numClasses; i++) {
                Class targetClass = targetClasses.get(i);

                if (targetClasses.lastIndexOf(targetClass) > i) {
                    addMockedTypeToMatchOnInstance(targetClass);
                }
            }
        }
    }

    private void addMockedTypeToMatchOnInstance(@NonNull Class mockedType) {
        if (!containsReference(mockedTypesToMatchOnInstances, mockedType)) {
            mockedTypesToMatchOnInstances.add(mockedType);
        }
    }

    boolean isToBeMatchedOnInstance(@NonNull Object mock) {
        Class mockedClass = GeneratedClasses.getMockedClass(mock);
        return containsReference(mockedTypesToMatchOnInstances, mockedClass);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy