Question

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.

Was it helpful?

Solution

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()

OTHER TIPS

 EasyMock.expectLastCall() 

or

 EasyMock.expectLastCall().anyTimes() 

or

 EasyMock.expectLastCall().andAnswer(..)

is not present in your code, must be after the method you want to test this is in case your test method is a void method.

otherwise you can use :

expect(CommandLineOptionProcessor.getOpts(args)).andReturn(object);

also please add this to you test class :

  @ObjectFactory
public IObjectFactory getObjectFactory() {

    return new org.powermock.modules.testng.PowerMockObjectFactory( );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top