mockit.internal.faking.FakeInvocation 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 automated developer testing.
It contains mocking/faking APIs and a code coverage tool, 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.
/*
* Copyright (c) 2006 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.internal.faking;
import java.lang.reflect.*;
import javax.annotation.*;
import mockit.internal.*;
import mockit.internal.state.*;
import static mockit.internal.util.Utilities.*;
/**
* An invocation to a {@linkplain mockit.Mock fake} method.
*/
public final class FakeInvocation extends BaseInvocation
{
@Nonnull private final FakeState fakeState;
@Nonnull private final String fakedClassDesc;
@Nonnull private final String fakedMethodName;
@Nonnull private final String fakedMethodDesc;
boolean proceeding;
@Nonnull // called by generated bytecode
public static FakeInvocation create(
@Nullable Object invokedInstance, @Nullable Object[] invokedArguments,
@Nonnull String fakeClassDesc, int fakeStateIndex,
@Nonnull String fakedClassDesc, @Nonnull String fakedMethodName, @Nonnull String fakedMethodDesc)
{
Object fake = TestRun.getFake(fakeClassDesc);
FakeState fakeState = TestRun.getFakeStates().getFakeState(fake, fakeStateIndex);
Object[] args = invokedArguments == null ? NO_ARGS : invokedArguments;
return new FakeInvocation(invokedInstance, args, fakeState, fakedClassDesc, fakedMethodName, fakedMethodDesc);
}
FakeInvocation(
@Nullable Object invokedInstance, @Nonnull Object[] invokedArguments, @Nonnull FakeState fakeState,
@Nonnull String fakedClassDesc, @Nonnull String fakedMethodName, @Nonnull String fakedMethodDesc)
{
super(invokedInstance, invokedArguments, fakeState.getTimesInvoked());
this.fakeState = fakeState;
this.fakedClassDesc = fakedClassDesc;
this.fakedMethodName = fakedMethodName;
this.fakedMethodDesc = fakedMethodDesc;
}
@Nonnull @Override
protected Member findRealMember()
{
Object invokedInstance = getInvokedInstance();
if (invokedInstance != null) {
Class> mockedClass = invokedInstance.getClass();
return fakeState.getRealMethodOrConstructor(mockedClass, fakedMethodName, fakedMethodDesc);
}
return fakeState.getRealMethodOrConstructor(fakedClassDesc, fakedMethodName, fakedMethodDesc);
}
boolean shouldProceedIntoConstructor()
{
if (proceeding && getInvokedMember() instanceof Constructor) {
fakeState.clearProceedIndicator();
return true;
}
return false;
}
@Override
public void prepareToProceed()
{
fakeState.prepareToProceed(this);
proceeding = true;
}
public void prepareToProceedFromNonRecursiveMock()
{
fakeState.prepareToProceedFromNonRecursiveFake(this);
proceeding = true;
}
@Override
public void cleanUpAfterProceed()
{
fakeState.clearProceedIndicator();
}
}