开发者

Need some help with some basic PowerMock / EasyMock problems

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 g开发者_JAVA百科et 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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜