扩展方法的 PowerMock 问题
我正在尝试使用 PowerMock 来模拟一些第三方代码,但我在扩展方法方面遇到了问题。
所以我将给出一个片段来显示正在发生的事情。
ClassA extends ClassB{
super();
}
ClassB extends ClassC{
super();
}
ClassC {
String methodA();
}
现在我尝试模拟 ClassA,因为这就是我的代码正在使用的。模拟创建得很好,但是当我添加这样的期望时:
expect(mockClassA.methodA()).andReturn("string");
我收到以下错误:
java.lang.IllegalStateException:缺少前面方法调用 methodA() 的行为定义 在 org.easymock.internal.MockInitationHandler.invoke(MockInitationHandler.java:43) 在 org.powermock.api.easymock.internal.inspirationcontrol.EasyMockMethodInvocalControl.invoke(EasyMockMethodInitationControl.java:95) 在 org.powermock.core.MockGateway.doMethodCall(MockGateway.java:104) 在 org.powermock.core.MockGateway.methodCall(MockGateway.java:167) 在.ClassC.methodA(ClassC.java)
对我所缺少的有什么想法吗?我知道我没有包含太多细节,但我已经使用符号准备了 ClassA 进行测试,我也只将重放放在一个地方,以确保在设置期望之前我不会错误地将 mockClassA 放入错误的状态。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我做了类似的事情,它对我有用,但是我不明白为什么你需要 PowerMock (你可以使用 EasyMock/Mockito 不用它来做到这一点)。
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).