扩展方法的 PowerMock 问题

发布于 2024-10-05 08:30:33 字数 971 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶情人眼里出诗心の 2024-10-12 08:30:33

我做了类似的事情,它对我有用,但是我不明白为什么你需要 PowerMock (你可以使用 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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文