Expectations on static void methods?

6,419 views
Skip to first unread message

Ride

unread,
Jan 9, 2009, 8:42:37 AM1/9/09
to PowerMock
Hi

I want to mock a static void method using easymock integration, is
this possible?

I modified IdGenerator.generateNewId() to make it a void method and
the following line from ServiceRegistratorTest now gives the error
below.

expect(IdGenerator.generateNewId()).andReturn(expectedId);

"The method expect(T) in type EasyMock is not applicable for the
arguments (void)"

Am I missing something or is this not supported, and if not will it
be?

Thanks

Ride

Johan Haleby

unread,
Jan 9, 2009, 9:36:37 AM1/9/09
to powe...@googlegroups.com
You just invoke the method then do expectLastCall().once() for example, e.g.

MyClass.myVoidMethod();
EasyMock.expectLastCall().once();

/Johan

Manuel Fernández Sánchez de la Blanca

unread,
Jan 9, 2009, 10:16:57 AM1/9/09
to powe...@googlegroups.com
Hi, everybody.

I've got another question about void methods. Is it possible to mock a
void method to do nothing?

Thanks.

Johan Haleby

unread,
Jan 9, 2009, 1:12:46 PM1/9/09
to powe...@googlegroups.com
Well mocking a void method makes it do nothing if that's what you want.
I think you mean that you'd like to stub the method call. You can do
that with plain EasyMock by creating a nice mock, e.g.

MyClass mock = createNiceMock(MyClass.class);

Now all method calls to that mock is stubbed by default.

Another solution would be to use the suppressMethod functionality of the
PowerMock API.

/Johan

Ride

unread,
Jan 10, 2009, 6:47:33 AM1/10/09
to PowerMock
Hi Johan

That works but only until I reset the mock, the following test case
gives this error

"java.lang.IllegalStateException: no last call on a mock available"

on the last line.

Ride

--------------------------------------------------------------------------------------

package staticvoid;

public class StaticVoid
{
public static void doNothing()
{
}
}

--------------------------------------------------------------------------------------

package staticvoid;

import static org.powermock.api.easymock.PowerMock.*;
import org.easymock.classextension.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticVoid.class)
public class StaticVoidTest
{
@Test
public void testname() throws InterruptedException
{
mockStatic(StaticVoid.class);

StaticVoid.doNothing();
EasyMock.expectLastCall().once();

replay(StaticVoid.class);

StaticVoid.doNothing();

verify(StaticVoid.class);

reset(StaticVoid.class);

StaticVoid.doNothing();
EasyMock.expectLastCall().once();
}
}




On Jan 9, 2:36 pm, "Johan Haleby" <johan.hal...@gmail.com> wrote:
> You just invoke the method then do expectLastCall().once() for example, e.g.
>
> MyClass.myVoidMethod();
> EasyMock.expectLastCall().once();
>
> /Johan
>

Ride

unread,
Jan 10, 2009, 7:07:37 AM1/10/09
to PowerMock
In fact it seems that Verify is the culprit, without verify i can
continue to reset the mock and andUse expectLastCall() an error is
thrown if the method is called too many times but there is no way to
check whether the method was called.

Ride

Johan Haleby

unread,
Jan 10, 2009, 8:12:27 AM1/10/09
to powe...@googlegroups.com
Thanks for reporting this, I'm practically never using reset myself so that could be a reason for why we haven't spotted this. We've had issues with verify before (because we're keeping state so that you can use replayAll, verifyAll and resetAll) but I thought they were fixed by now.. Anyway I'll try to have a look at it tomorrow when I have some more time.

/Johan

Johan Haleby

unread,
Jan 10, 2009, 6:02:41 PM1/10/09
to powe...@googlegroups.com
This was indeed a bug of the verification behavior in PowerMock. I think
I've fixed it now and committed it to trunk. If you like please check
out the source and build the project using "mvn install" and see if it
solves your problems.

/Johan

Ride

unread,
Jan 13, 2009, 5:40:23 AM1/13/09
to PowerMock
Thanks

I'm using ant instead of maven, is there somedocumentation about how
to build the project manually?

Piers
> >>> ---------------------------------------------------------------------------­-----------
>
> >>> package staticvoid;
>
> >>> public class StaticVoid
> >>> {
> >>>         public static void doNothing()
> >>>         {
> >>>         }
>
> >>> }
>
> >>> ---------------------------------------------------------------------------­-----------

Johan Haleby

unread,
Jan 13, 2009, 5:46:29 AM1/13/09
to powe...@googlegroups.com
To build PowerMock you basically need maven2. After that you can use ant or ivy or whatever you like when you use PowerMock. So I suggest that you download maven 2 and just do "mvn install" in the root folder of PowerMock.If you think it's too much of a problem I guess we'll release a new version pretty soon (before February since I'm going on a vacation to India for 2,5 months) which includes this fix.

/Johan
Reply all
Reply to author
Forward
0 new messages