Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powermock (With Easymock) no last call on a mock available

I am trying to just run a simple test case. I have the following method.

public static void run(String[] args) throws Throwable {
    CommandLineArguments opts = CommandLineOptionProcessor.getOpts(args);
}

I will continue to build this method / test case as I go. However I just wanted to make sure a simple test case worked first. So I wrote the following test.

@Test
public void testRun() {
    String[] args = {"--arg1", "value", "--arg2", "value2"};

    mockStatic(CommandLineOptionProcessor.class);
    expect(CommandLineOptionProcessor.getOpts(args));

    EasyMock.replay(CommandLineOptionProcessor.class);
}

After that I get the following error:

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

I read some of the other posts on StackOverflow but their solution seemed to be that they were using PowerMock with Mockito. I am using Powermock and Easymock, so that should not be the problem.

I followed Rene's advice and added the following to the top of my class.

@PrepareForTest(CommandLineOptionProcessor.class)
@RunWith(PowerMockRunner.class)
public class DataAssemblerTest {

I fixed the previous error. But now I have this error.

java.lang.IllegalArgumentException: Not a mock: java.lang.Class
at org.easymock.internal.ClassExtensionHelper.getControl(ClassExtensionHelper.java:61)
at org.easymock.EasyMock.getControl(EasyMock.java:2172)
at org.easymock.EasyMock.replay(EasyMock.java:2074)
.
.
.

Any ideas on what could be causing this would be great.

like image 745
dev Avatar asked Nov 21 '13 14:11

dev


1 Answers

Did you annotate the test class with @RunWith(PowerMockRunner.class) and @PrepareForTest(CommandLineOptionProcessor.class)?

 @RunWith(PowerMockRunner.class)
 @PrepareForTest(CommandLineOptionProcessor.class)
 public class TestClass {

     @Test
     public void testRun(){

You need the @PrepareForTest(CommandLineOptionProcessor.class) at the test class level. See the Powermock doc:

Use the @PrepareForTest(ClassThatContainsStaticMethod.class) annotation at the class-level of the test case.

Also ensure that the required libraries are on the test classpath.

In your case the javassist library is missing. Put it on the classpath. Maybe some other libs are also missing... we will see.

If you get

java.lang.IllegalArgumentException: Not a mock: java.lang.Class

then you are using EasyMock.replay(), but you must use PowerMock.replay()

like image 67
René Link Avatar answered Sep 29 '22 10:09

René Link