Powermock (with Easymock) is not the last call to the layout

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 this method / test case when I go. However, I just wanted to make sure that the first 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 other posts on StackOverflow, but their solution seemed to use PowerMock with Mockito. I use Powermock and Easymock, so this should not be a problem.

I followed Rene’s advice and added the following to the beginning 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 might be causing this will be great.

+8
java unit-testing junit powermock easymock
source share
2 answers

Did you comment on the test class using @RunWith(PowerMockRunner.class) and @PrepareForTest(CommandLineOptionProcessor.class) ?

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

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

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

Also make sure the required libraries are in the test path.

In your case, the javassist library is missing . Put it on the class path. Perhaps some other libraries are also missing ... we'll see.

If you get

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

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

+16
source share
  EasyMock.expectLastCall() 

or

  EasyMock.expectLastCall().anyTimes() 

or

  EasyMock.expectLastCall().andAnswer(..) 

not in your code, there should be after the method that you want to test this in case your testing method is void.

otherwise you can use:

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

also please add this to your test class:

  @ObjectFactory public IObjectFactory getObjectFactory() { return new org.powermock.modules.testng.PowerMockObjectFactory( ); } 
0
source share

All Articles