mock private static method with EasyMock.isA - Unexpected method call...

3,046 views
Skip to first unread message

Quentin

unread,
Jan 25, 2011, 5:28:02 AM1/25/11
to PowerMock
Hello,
I want to mock a private static method of a class, and I want this
mock to be used when invoked with every object of the class "AClass".

Here is a very little example of the TestCase.
For me, when invoking private method Mocked.methodToBeMocked(AClass),
it should return "OOOK" as defined in the expectPrivate method.
I don't know why, but I get the following error :
Unexpected method call theTest():
methodToBeMocked(isA(commande.TestMock$AClass)): expected: 1,
actual: 0

Any idea ?
Thanks for your help,

Quentin

@RunWith(PowerMockRunner.class)
public class TestMock {
private String LINE_EXPECTED = "OOOK";
@Test
@PrepareForTest(Mocked.class)
public void test () {
try {
PowerMock.mockStatic(Mocked.class);
Mocked instance = PowerMock.createPartialMock(Mocked.class,
"methodToBeMocked");
PowerMock.expectPrivate(instance, "methodToBeMocked",
EasyMock.isA(AClass.class)).andReturn(LINE_EXPECTED);
PowerMock.replay(Mocked.class);
String result = Mocked.theTest();
Assert.assertEquals("what's wrong ?", LINE_EXPECTED, result);
} catch (Exception e) {
Assert.fail(e.getMessage());
}

PowerMock.verify(Mocked.class);
}
/**
* A class where I want to mock "methodToBeMocked"
*/
public static class Mocked {
public static String theTest () {
AClass aClass = new AClass();
return methodToBeMocked (aClass);
}
private static String methodToBeMocked (AClass aclass) {
return "KO";
}
}

public static class AClass {
}
}


full error :

java.lang.AssertionError:
Unexpected method call theTest():
methodToBeMocked(isA(commande.TestMock$AClass)): expected: 1,
actual: 0
at
org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:
45)
at
org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl.invoke(EasyMockMethodInvocationControl.java:
95)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)
at org.powermock.core.MockGateway.methodCall(MockGateway.java:60)
at commande.TestMock$Mocked.theTest(TestMock.java)
at commande.TestMock.test(TestMock.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:
322)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:
86)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:
94)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:
309)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl
$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:
112)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl
$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:
73)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:
297)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:
222)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:
161)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$1.run(PowerMockJUnit44RunnerDelegateImpl.java:135)
at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:
34)
at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:
44)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:
133)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:
112)
at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:
57)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
49)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:
38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
197)


Johan Haleby

unread,
Jan 25, 2011, 6:41:11 AM1/25/11
to powe...@googlegroups.com
Hi,

I think it would be much simpler for you to use the stubbing API. Then you simply write:

stub(method(Mocked.class, "methodToBeMocked")).toReturn(LINE_EXPECTED);

This way you don't need to fiddle around with EasyMock matchers.

/Johan



--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.


Quentin Ambard

unread,
Jan 25, 2011, 7:40:19 AM1/25/11
to powe...@googlegroups.com
Hi,
Thanks for your help, I didn't thought about stubbing API, and it fits my needs better than mocks.

Btw, I found a solution for my previous example : I had to use "mockStaticPartial" instead of "mockStatic", since theTest() method isn't mocked :
PowerMock.mockStaticPartial(Mocked.class, "methodToBeMocked");

Regards,

Quentin

2011/1/25 Johan Haleby <johan....@gmail.com>



--
Quentin Ambard
Reply all
Reply to author
Forward
0 new messages