PowerMock:: [java.lang.IllegalStateException: no last call on a mock available]

16,856

Solution 1

Your code is missing the annotation

@PrepareForTest(IdGenerator.class)

Solution 2

In my case I was missing the following method in my test class

   @ObjectFactory
   /**
    * Configure TestNG to use the PowerMock object factory.
    */
   public IObjectFactory getObjectFactory() {
      return new org.powermock.modules.testng.PowerMockObjectFactory();
   }

Once I added it, I got rid of the "no last call on a mock available" error.

Solution 3

You need to put the replay before the actual call to the method.

EDIT: I think part of the problem may be caused because of your imports. Try not to import static powermock and static easymock (I've found that I often confuse myself and forget which one I need to call replay on).

Try running the following code. If it doesn't run correctly, then it may be because of a problem with the particular version of PowerMock/EasyMock/Junit that you have.

TestClass:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.easymock.EasyMock.*;

import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest(IdGenerator.class)
public class TestClass {

@Test
public void testRegistrarService()
{
    ServiceRegistrator serTestObj = new ServiceRegistrator();

    PowerMock.mockStatic(IdGenerator.class);
    expect(IdGenerator.generateNewId()).andReturn(42L);
    PowerMock.replay(IdGenerator.class);
    long actualId=serTestObj.registerService();
    PowerMock.verify(IdGenerator.class);
    assertEquals(42L,actualId);
 }
}

IdGenerator:

public class IdGenerator {
     public static long generateNewId()
      {
        return System.currentTimeMillis();
      }
}

ServiceRegistrator:

public class ServiceRegistrator {
    public long registerService()
    {
        long id = IdGenerator.generateNewId();
        return id;
     }
}
Share:
16,856
SachG
Author by

SachG

Updated on July 15, 2022

Comments

  • SachG
    SachG almost 2 years

    To mock a static method powermock giving an exception while expect().

    @Test
    public void testRegistrarService()
    {
       mockStatic(IdGenerator.class);
       expect(IdGenerator.generateNewId()).andReturn(42L);
       long actualId=serTestObj.registerService();
       replay(IdGenerator.class);
       verify(IdGenerator.class);
       assertEquals(42L,actualId);
     }
    
    
    public class ServiceRegistrator
    {
    public long registerService()
    {
        long id = IdGenerator.generateNewId();
        return id;
     }
    }
    
    public class IdGenerator
    {
      public static long generateNewId()
      {
        return System.currentTimeMillis();
      }
    }
    

    Exception is:

    java.lang.IllegalStateException: no last call on a mock available
    at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:521)
    at org.easymock.EasyMock.expect(EasyMock.java:499)
    at  home.powermock.testServiceRegistrator.testRegistrarService(testServiceRegistrator.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:31)
    

    how to mock staic method,while m using powerMock i'm using intelliJ idea,how to resolve that exception.

  • SachG
    SachG almost 11 years
    @RunWith(PowerMockRunner.class) @PrepareForTest({testServiceRegistrator.class,IdGenerator.cl‌​ass} i already added both annotation before class in which test method is present.
  • JoseK
    JoseK almost 11 years
    remove the testServiceRegistrator.class from the list
  • SachG
    SachG almost 11 years
    removed...still the same exception.. :( now i have like @PrepareForTest(IdGenerator.class).. i think that the root cause is in "expect()" method.Bcz exception is coming at that point.may be some thing is cooking here. but not able to resolve it?
  • SachG
    SachG almost 11 years
    HardcoreBro i tried your approach.but still the same exception..still not resolved... :(
  • HardcoreBro
    HardcoreBro almost 11 years
    See the revised post. I added source code. I think your problem may be with the way you imported your code... Try changing your imports to match the above code and let me know how it goes
  • SachG
    SachG almost 11 years
    still its not working same exception is coming....is this code working on your environment????? If so it seems to be a jar problem...then please let me know once which jar are you using for EasyMock/PowerMock/JUnit....
  • HardcoreBro
    HardcoreBro almost 11 years
    The code works in my environment. I'm going to list everything that I have imported, but there might be something that I'm using that isn't absolutely necessary: powermock-api-easymock-1.4.10.jar hamcrest-core-1.3.jar junit--4.11.jar platform.jar powermock-module-junit4-1.4.10.jar powermock-module-junit4-common-1.4.10.jar powermock-core-1.4.10.jar powermock-api-support-1.4.10.jar easymock-3.0.jar javassist-3.15.0-GA.jar powermock.reflect-1.4.10.jar objenesis-1.2.jar cglib-nodep-2.2.jar
  • SachG
    SachG almost 11 years
    HardCoreBro..i'm using almost same jar.but don't know why still facing same exception....Dear Can you please mail me this all jar to my gmail id.... "[email protected]" ...please mail all the impoted jars..thanks
  • Sandeep
    Sandeep over 8 years
    facing same issue even after putting @PrepareForTest
  • Chris
    Chris over 7 years
    In my case, it got rid of the original error message and replaced it with "Inconsistent stackmap frames at branch target 49" ... I'd love to hear a bit more about the reasoning behind adding the IObjectFactory.
  • Chris
    Chris over 7 years
    I'm using testng, not junit and added the RunWith. I took that out and adding the ObjectFactory seems to have me on my way. I got some insight here: github.com/jayway/powermock/wiki/TestNG_usage