BUG - Powermock counts more than actually call when verify

83 views
Skip to first unread message

焦晓冬

unread,
Oct 10, 2015, 11:11:02 AM10/10/15
to PowerMock
Hi, hackers of Powermock

Is it a bug of Powermock or I'm doing sth wrong?

The following test should pass, but failed with:
trackBugPartialMockCountMore(com.xiaomi.finddevice.test.testcase.PowerMockBug)
org.mockito.exceptions.verification.TooManyActualInvocations: 
classToMock.foo();
Wanted 1 time:
-> at com.xiaomi.finddevice.test.testcase.PowerMockBug.trackBugPartialMockCountMore(PowerMockBug.java:24)
But was 3 times. Undesired invocation:
-> at com.xiaomi.finddevice.test.testcase.PowerMockBug.trackBugPartialMockCountMore(PowerMockBug.java:22)
When I remove @PrepareForTest(ClassToMock.class), every thing goes well and the test get passed.

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassToMock.class)
public class PowerMockBug {

    @Test
    public void trackBugPartialMockCountMore() {
        ClassToMock mock = mock(ClassToMock.class);
        when(mock.foo()).thenCallRealMethod();

        mock.foo();

        verify(mock).foo();
    }

}


class ClassToMock {
    public int foo() { return 0x10; }
}

Johan Haleby

unread,
Oct 11, 2015, 1:52:33 AM10/11/15
to powe...@googlegroups.com
Hi, 

Try:

doCallRealMethod().when(mock).foo();

Instead of:

 when(mock.foo()).thenCallRealMethod();

Regards,
/Johan

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.

焦晓冬

unread,
Oct 12, 2015, 2:19:31 AM10/12/15
to PowerMock
Using PowerMock.doCallRealMethod(mock).foo() gives exactly the same verify failure.

Wanted 1 time:
-> at com.xiaomi.finddevice.test.testcase.PowerMockBug.trackBugPartialMockCountMore(PowerMockBug.java:27)
But was 3 times. Undesired invocation:
-> at com.xiaomi.finddevice.test.testcase.PowerMockBug.trackBugPartialMockCountMore(PowerMockBug.java:25)
 

在 2015年10月11日星期日 UTC+8下午1:52:33,Johan Haleby写道:

焦晓冬

unread,
Oct 12, 2015, 2:21:23 AM10/12/15
to PowerMock
Sorry,

I mean,

Using PowerMock.doCallRealMethod().when(mock).foo() gives exactly the same verify failure.

VERSION: powermock-mockito-junit-1.6.3

在 2015年10月12日星期一 UTC+8下午2:19:31,焦晓冬写道:

Johan Haleby

unread,
Oct 12, 2015, 1:48:49 PM10/12/15
to powe...@googlegroups.com
Hmm sounds like a bug. Would be great if you could help out and provide a pull request.
Reply all
Reply to author
Forward
0 new messages