19

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.

2
  • No. I am currently not using Maven.
    – dev
    Nov 21, 2013 at 14:42
  • @RenéLink I fixed the issue with JavaAssist. Turns out I just didn't have the Library in the project. So that is fine now. However, I now have this new issue that says 'Not a mock'.
    – dev
    Nov 21, 2013 at 14:52

2 Answers 2

25

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

2
  • Oh okay. I had tried @PrepareForTest but I had done it at the method-level. When I had @PrepareForTest(CommandLineOptionProcessor.class) and @RunWith(PowerMockRunner.class) to the Class Level I now get a new error. I will edit my Original Post with the error.
    – dev
    Nov 21, 2013 at 14:37
  • 1
    Yeah I just found that as well. I had to make one more change to my test (but that was just my test failing). It works great now. Thanks for all the help and the walkthrough of Mocking a static method w/ Powermock. Def. a bit confusing when you're new to it.
    – dev
    Nov 21, 2013 at 14:58
-1
 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( );
}
2
  • What does adding that to the test class do?
    – dev
    Nov 21, 2013 at 15:03
  • 1
    I had an issue - an exception when tried to mock static classes and this helped me. Nov 21, 2013 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.