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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With