需要一些有关 PowerMock / EasyMock 基本问题的帮助

发布于 2024-10-21 17:11:11 字数 1510 浏览 2 评论 0原文

我对 PowerMock / EasyMock 的世界还比较陌生,我认为应该相对简单的东西却不起作用。也许有人可以告诉我哪里出错了。

考虑这两个类:

public class Foo
{
    public int doStuff(int a)
    {
        return (5 / a);
    }
}

public class Bar
{
    public void doSomething()
    {
        Foo foo = new Foo();
        int c = foo.doStuff(0);
    }
}

现在,在测试类中,我想测试 Bar 方法 doSomething。我遇到的问题是它对 Foo 的使用。正如您所看到的,将 0 传递给 Foo 将导致除以零错误。我想删除/抑制 Foo.doStuff() 这样就不会发生这种情况。我真正需要做的就是确保 doStuff() 实际上被调用。所以,我这样做了:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Foo.class })                 

public class TestStuff
{

@Test
public void testOtherStuff() throws Exception
{

    Foo fooMock = PowerMock.createMock(Foo.class);
    expectNew(Foo.class).andReturn(fooMock);
    EasyMock.expect(fooMock.doStuff(anyInt())).andReturn(1);
    //fooMock.doStuff(anyInt());

    //suppress (method(Foo.class, "doStuff"));

    replayAll();

    Bar bar = new Bar();
    bar.doSomething();

    verifyAll();
}
}

当我运行这个时,我得到了除以零的异常。我原以为使用expect() 调用并指定返回值1 会阻止该方法执行。显然情况并非如此。所以第一个问题是,为什么不呢?

然后我尝试执行上面注释掉的语句。也就是说,我注释掉了expect()调用,并使用了其他两个语句,认为我必须抑制该方法的执行。我发现如果我不执行suppress()语句,我总是会得到除以零的异常。但是,通过在其中使用抑制语句,我收到一个断言错误,其中显示 doStuff 预计被调用 1 次,而实际调用次数为 0 次。

那么,为什么该方法不被计为已执行呢?

如何让模拟的东西“存根”该方法,以便它算作是由实际上不执行任何操作的执行?

我一直在处理的情况实际上对 doStuff 没有回报。对于处理无返回值的方法和有返回值的方法,必要的模拟语句有何不同?

看起来我正处于完成这项工作的风口浪尖,但我觉得我在这里尝试了某种不正确的语句组合。我已经对此深有体会,读了很多例子,但仍然不够。

有什么帮助吗?

谢谢,

克雷格

I'm relatively new to the world of PowerMock / EasyMock, and something I thought should be relatively straight-forward just isn't working. Perhaps someone can show me where I'm going wrong.

Consider these two classes:

public class Foo
{
    public int doStuff(int a)
    {
        return (5 / a);
    }
}

public class Bar
{
    public void doSomething()
    {
        Foo foo = new Foo();
        int c = foo.doStuff(0);
    }
}

Now, in the test class, I want to test the Bar method doSomething. The problem I've got is its use of Foo. As you can see, passing a 0 to Foo will result in a divide by zero error. I'd like to stub out / suppress Foo.doStuff() so that this doesn't happen. All I really need to do is ensure that doStuff() actually gets called. So, I've done this:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Foo.class })                 

public class TestStuff
{

@Test
public void testOtherStuff() throws Exception
{

    Foo fooMock = PowerMock.createMock(Foo.class);
    expectNew(Foo.class).andReturn(fooMock);
    EasyMock.expect(fooMock.doStuff(anyInt())).andReturn(1);
    //fooMock.doStuff(anyInt());

    //suppress (method(Foo.class, "doStuff"));

    replayAll();

    Bar bar = new Bar();
    bar.doSomething();

    verifyAll();
}
}

When I run this I've getting the divide by zero exception. I had thought that using the expect() call, and specifying the return value of 1 would prevent the method from being executed. This is obviously not the case. So the first question is, why not?

So then I tried doing the statements that are commented out above. That is, I commented out the expect() call, and used the other two statements, thinking that I had to suppress execution of the method. I found that if I didn't do the suppress() statement I would always get the divide by zero exception. However, by having the suppress statement in there, I get an assertionerror, where it says that doStuff was expected to be called 1 time, and the actual calls were 0.

So, why isn't the method counted as executed?

How do I get the mock stuff to "stub" the method so that it counts as being executed by doesn't really do anything?

The situation I've been dealing with actually has a void return on doStuff. How to the necessary mock statements differ for handling methods with no return value, versus those that do?

It seems like I'm on the cusp of making this work, but I feel like I've got some kind of incorrect combination of statements being tried here. I've hit my head on this, read a bunch of examples, and I'm still coming up short.

Any help?

Thanks,

Craig

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

鼻尖触碰 2024-10-28 17:11:11

从 PowerMock 文档中查看:http://code.google.com/p/powermock /wiki/MockConstructor

类似于:

Foo fooMock = createMock(Foo.class);
expectNew(Foo.class, 0).andReturn(fooMock);

编辑: 添加文档中真正相关的部分(克雷格自己找到的):

使用@PrepareForTest(ClassThatCreatesTheNewInstance.class)
类级别的注释
测试用例。

Look at this from the PowerMock docs: http://code.google.com/p/powermock/wiki/MockConstructor

Something like:

Foo fooMock = createMock(Foo.class);
expectNew(Foo.class, 0).andReturn(fooMock);

Edit: Adding the real relevant part of the docs (which Craig found on his own):

Use the @PrepareForTest(ClassThatCreatesTheNewInstance.class)
annotation at the class-level of the
test case.

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