Wednesday, April 18, 2012

Mocking Statics and java.lang.IllegalStateException: no last call on a mock available

With the help of PowerMock it is now much more easier to mock static methods or partial mock of an object (not interface).

On my first trial I recieved an error saying "java.lang.IllegalStateException: no last call on a mock available".

In fact; as stated on the document it should be enough to add @RunWith(PowerMockRunner.class) and @PrepareForTest(MyClassThatHasStaticMethof.class) annotations prior to class defination.

It is also stated on here.

But I continue to see the same error. At the end I noticed that although I was using EasyMock; I was using Mockito imports:

You should use "import static org.powermock.api.easymock.PowerMock.*" instead of "import static org.powermock.api.mockito.PowerMockito.*"

It was my fault to choose the first import recommendation by ide. Don't forget to be sure you are importing the right package.

3 comments: