Re: [powermock] Mocking static class/fn is throwing exception

1,804 views
Skip to first unread message

Johan Haleby

unread,
Feb 5, 2013, 2:15:05 AM2/5/13
to powe...@googlegroups.com
Hi,

Could you give us an example of the class and methods you're trying to test as well as the test case?

Regards,
/Johan

On Mon, Feb 4, 2013 at 3:22 PM, Atul Parti <atul...@gmail.com> wrote:
I have a class to be tested. Inside that i have a fn which call a static fn.
In the test class I added @RunWith(PowerMockRunner.class)
@PrepareForTest(StaticClass.class) and PowerMockito.mockStatic(StaticClass.class);
to mock the static class. But the moment I call this Mockito.when(StaticClass.staticCall(12)).thenReturn(5); i get following error

org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
3. the parent of the mocked class is not public.
   It is a limitation of the mock engine.

    at oracle.demo.unit.test.with.mocklibrary.SampleTestClassMockito.testSampleClassForValidValues(SampleTestClassMockito.java:56)
    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:597)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:243)
    at junit.framework.TestSuite.run(TestSuite.java:238)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    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)



This is when i am trying with MockiTo. With Easymock it worked. Any help will be appriciated.

--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Johan Haleby

unread,
Feb 6, 2013, 1:53:51 PM2/6/13
to powe...@googlegroups.com
This is the way Mockito works.

On Wed, Feb 6, 2013 at 4:53 PM, Kirill Malyshev <keryas...@gmail.com> wrote:
after some experiments with Powermock+Mockito and static methods i found that best way is like this:

@PrepareForTest(StaticClass.class) // on class or maybe method level
......
PowerMockito.mockStatic(StaticClass.class);
PowerMockito.doReturn(5).when(StaticClass.class);
StaticClass.staticCall(12);

though it looks a bit weird, but it doesn't have a side effect of executing the method body at the time of when(....), while idiom PowerMockito.when(StaticClass.staticCall(12)).thenReturn(...) does
Reply all
Reply to author
Forward
0 new messages