Question

I am attempting to use PowerMock to mock some third party code and I am having an issue with an extended method.

So I will give a snippet showing what is occuring.

ClassA extends ClassB{
     super();
}

ClassB extends ClassC{
     super();
}

ClassC {
     String methodA();
}

Now I am attempting to mock ClassA as that is what my code is using. The mock creates fine, however when I add an expectation like so:

expect(mockClassA.methodA()).andReturn("string");

I get the following error:

java.lang.IllegalStateException: missing behavior definition for the preceding method call methodA() at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:43) at org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl.invoke(EasyMockMethodInvocationControl.java:95) at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:104) at org.powermock.core.MockGateway.methodCall(MockGateway.java:167) at .ClassC.methodA(ClassC.java)

Any thoughts on what I am missing? I know I haven't included much detail, but I have prepared ClassA for test using the notation, I have also only put the replay in one place to ensure that I am not incorrectly putting mockClassA into the wrong state before setting the expectation.

Was it helpful?

Solution 2

The exception i was getting was a result of poor expectations rather than anything to do with the class extension. Sorry for the mis-stated question.

OTHER TIPS

I did something like this and it works for me, however I dont understand why you need PowerMock here(you can do that without it with EasyMock/Mockito).

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassA.class)
public class ClassATest {
    @Test
    public void finalMethodString() throws Exception {
        ClassA f = PowerMock.createNiceMock(ClassA.class);
        EasyMock.expect(f.methodA()).andReturn("haha");
        EasyMock.replay(f);
        assertEquals("haha1", f.methodA());
    }
}


class ClassA extends ClassB{
    @Override
    String methodA() {
        return "1";
    }
}
class ClassB extends ClassC{
    @Override
    String methodA() {
        return "b";
    }
}
class ClassC {
    String methodA() {
        return null;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top