tiebin-zhang / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

PowerMock.mockStatic does not work with RETURN_DEEP_STUBS #487

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I'll try the following (quite simple) static method mocking in my test:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ FacesContext.class })
public class MyControllerTest {

@Test
public final void testAnything() {
...
PowerMockito.mockStatic(FacesContext.class, RETURNS_DEEP_STUBS);
PowerMockito.when(FacesContext.getCurrentInstance().getExternalContext().getRequ
estLocale()).thenReturn(Locale.GERMANY);
...
}

I would expect that I can mock in this way the deep stubbed call to my locale, 
but I got the following error:

java.lang.NullPointerException
    at org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs.getMock(ReturnsDeepStubs.java:47)
    at org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs.answer(ReturnsDeepStubs.java:39)
    at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93)
    at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:260)
    at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:192)
    at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)
    at org.powermock.core.MockGateway.methodCall(MockGateway.java:60)
    at javax.faces.context.FacesContext.getCurrentInstance(FacesContext.java)
    at my.test.MyControllerTest.testAnything(MyControllerTest.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at ...

I'm using PowerMock in version 1.5.1 and Mockito in version 1.9.5.

Ok, I could find a quick workaround by calling:
...
PowerMockito.mockStatic(FacesContext.class);
FacesContext fContext = Mockito.mock(FacesContext.class, RETURNS_DEEP_STUBS);
PowerMockito.when(FacesContext.getCurrentInstance()).thenReturn(fContext);
Mockito.when(fContext.getExternalContext().getRequestLocale()).thenReturn(Locale
.GERMANY);
...

But in my point of view it is an error of PowerMock NOT to support deep stub 
calls for static methods, isn't it?!

Original issue reported on code.google.com by michael....@googlemail.com on 18 Mar 2014 at 12:49

GoogleCodeExporter commented 8 years ago
PowerMock has never supported deep stub calls if I can remember it correctly. 
Please help out by providing a patch.

Original comment by johan.ha...@gmail.com on 21 Mar 2014 at 6:13

GoogleCodeExporter commented 8 years ago
I also ran now into this problem. Very annoying as one except it to work 
similar to Mockito. Good to have this issue found so that I can move on getting 
a work around.

Original comment by xmarcus89@gmail.com on 27 Aug 2014 at 12:12

GoogleCodeExporter commented 8 years ago
Yeah, I found my own comment again by googling this issue, wonderful ... I 
still would love to see this fixed :(

Original comment by xmarcus89@gmail.com on 5 Dec 2014 at 9:19

GoogleCodeExporter commented 8 years ago
Dear PowerMock-Devs, what must be done to get RETURN_DEEP_STUBS working?  I and 
maybe other's want to send you pull requests with a fix.

 - Which classes are involved?
 - What should be considered?
 - What should not be done?
 - ...

Original comment by chriss....@gmail.com on 10 Mar 2015 at 8:00

GoogleCodeExporter commented 8 years ago
I think that the best way to find out is to look at the source code for 
non-static mocks and port it to static mocks as well.

Original comment by johan.ha...@gmail.com on 10 Mar 2015 at 10:32

GoogleCodeExporter commented 8 years ago
I try to find a solution in the discussion group-> 
https://groups.google.com/forum/#!topic/powermock/g9gV6cyR-Ng

Original comment by chriss....@gmail.com on 12 Mar 2015 at 8:28