![JAR search and dependency download from the Maven repository](/logo.png)
mockit.internal.state.MockInstances Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for developer (unit/integration) testing.
It contains mocking APIs and other tools, supporting both JUnit and TestNG.
The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested
in isolation from selected dependencies.
The newest version!
/*
* Copyright (c) 2006-2013 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.internal.state;
import java.util.*;
import org.jetbrains.annotations.*;
import mockit.*;
import mockit.internal.util.*;
/**
* Holds a list of instances of mock classes (either regular classes provided by client code, or startup mock classes
* provided internally by JMockit or by external jars).
*
* This is needed to allow each redefined real method to call the corresponding mock method on the instance of the mock
* class (unless the mock method is {@code static}).
*/
public final class MockInstances
{
@NotNull private final List> mocks = new ArrayList>();
int getInstanceCount() { return mocks.size(); }
@Nullable MockUp> getMock(int index) { return mocks.get(index); }
public int addMock(@NotNull MockUp> mock)
{
int i = Utilities.indexOfReference(mocks, mock);
if (i < 0) {
i = mocks.size();
mocks.add(mock);
}
return i;
}
void removeInstance(@NotNull MockUp> mock)
{
int i = Utilities.indexOfReference(mocks, mock);
if (i >= 0) {
mocks.set(i, null);
}
}
void removeInstances(int fromIndex)
{
for (int i = mocks.size() - 1; i >= fromIndex; i--) {
mocks.remove(i);
}
}
@Nullable MockUp> findMock(@NotNull Class> mockClass)
{
int n = mocks.size();
for (int i = 0; i < n; i++) {
MockUp> mock = mocks.get(i);
if (mock != null && mock.getClass() == mockClass) {
return mock;
}
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy