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

mockit.internal.faking.FakeInvocation 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.

There is a newer version: 1.49
Show newest version
/*
 * 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();
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy