Take the 2-minute tour ×

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:

@PrepareForTest({ Foo.class })                 

public class TestStuff

public void testOtherStuff() throws Exception

    Foo fooMock = PowerMock.createMock(Foo.class);

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


    Bar bar = new Bar();


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?



share|improve this question

1 Answer 1

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.

share|improve this answer
the testOtherStuff method already contains the calls createMock and expectNew that you describe. –  krock Mar 11 '11 at 23:59
Wow. It actually worked when I changed the PrepareForTest to use the Bar class instead of the Foo class. Apparently, I've been preparing the wrong class. Amazing. Thanks ever so much! –  Craig Anderson Mar 12 '11 at 0:16
@krock, very true. Bad me. I updated with the solution Craig found on his own, just so it's up here. –  Michael Brewer-Davis Mar 12 '11 at 0:23

Your Answer


