开发者

PowerMock issue with an extended method

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开发者_StackOverflow社区 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.


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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜