java.lang.IllegalStateException While mocking static method with powermock

551 views
Skip to first unread message

sachin grover

unread,
Jun 27, 2013, 8:17:31 AM6/27/13
to powe...@googlegroups.com
Hi,
    I using below jar to mock static method using Easy mock-Power-mock.
         easymock-3.1.jar , javassist-3.17.1-GA.jar,junit-4.8.2.jar,objenesis-1.2.jar,cglib-nodep-2.2.2.jar,powermock-easymock-1.5.1-full.jar

but I'm getting Exception while mocking

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


Here below is my tester class,exception snippet..

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.*;

Test class:

@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 class:

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

ServiceRegistrator class:

public class ServiceRegistrator {
    public long registerService()
    {
        long id = IdGenerator.generateNewId();
        return id;
     }
}


Exception Snippet::
java.lang.IllegalStateException: no last call on a mock available
    at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:520)
    at org.easymock.EasyMock.expect(EasyMock.java:498)
    at home.powermock.testServiceRegistrator.testRegistrarService(testServiceRegistrator.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)


please guide me how to resolve that exception..
Thanks

Regards,
Sachin

Johan Haleby

unread,
Jul 1, 2013, 1:56:52 AM7/1/13
to powe...@googlegroups.com
Hmm can't seem to find anything wrong with the code. When I run this it works.

/Johan



--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

sachin grover

unread,
Jul 1, 2013, 5:44:47 AM7/1/13
to powe...@googlegroups.com
john,
        but when i run the same on my environment .its giving above exception.I'm using intelliJ IDEA.i also tried on Net Beans.

can you please tell me the jar[whatever i mentioned]  is correct?

or please attach jars which are you using for the same code into this mail.


Thanks.

Johan Haleby

unread,
Jul 1, 2013, 6:44:45 AM7/1/13
to powe...@googlegroups.com
There's a similar test in the tutorial which you can find here. It's a maven project (the root pom of the project is here). If you just open the pom.xml file in intellij you ought to be able to see what jars are in the classpath and in which order. How ever it should be enough to download the bundle from the project page.

/Johan


--
Reply all
Reply to author
Forward
0 new messages