tiebin-zhang / powermock

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

powermock causes Mockito's initMocks to fail when using @Mock #414

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Here is my class under test:

public class MockMe {

    private static final MockMe ME = new MockMe();

    public static MockMe getInstance() {
            return ME;
    }

}

And here is my test:

import static org.mockito.MockitoAnnotations.initMocks;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.testng.Assert.assertEquals;

import org.powermock.core.classloader.annotations.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockObjectFactory;
import org.testng.IObjectFactory;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;

@PrepareForTest({MockMe.class})
public class TestTest {

    @ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new PowerMockObjectFactory();
    }

    @Mock
    public MockMe mockMe;

    @BeforeTest
    public void init() {
        initMocks(this);
        mockStatic(MockMe.class);
        when(MockMe.getInstance()).thenReturn(mockMe);
    }

    @Test
    public void sampleTest() {
        MockMe mockMe1 = MockMe.getInstance();
        assertEquals(mockMe1, mockMe);
    }
}

When I run this test it succeeds but If I replace the deprecated 

org.powermock.core.classloader.annotations.Mock

with the appropriate

org.mockito.Mock

The test fails with the following stack trace:

FAILED CONFIGURATION: @BeforeTest init
java.lang.ExceptionInInitializerError
    at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
    at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30)
    at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:19)
    at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
    at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:145)
    at org.mockito.internal.configuration.ClassPathLoader.findPluginImplementation(ClassPathLoader.java:110)
    at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:106)
    at org.mockito.internal.configuration.ClassPathLoader.<clinit>(ClassPathLoader.java:59)
    at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:21)
    at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:40)
    at org.mockito.internal.stubbing.defaultanswers.ReturnsMocks.<init>(ReturnsMocks.java:18)
    at org.mockito.Answers.<clinit>(Answers.java:52)
    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 java.lang.Class.getEnumConstantsShared(Class.java:2942)
    at java.lang.Class.enumConstantDirectory(Class.java:2963)
    at java.lang.Enum.valueOf(Enum.java:191)
    at sun.reflect.annotation.AnnotationParser.parseEnumValue(AnnotationParser.java:413)
    at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:278)
    at java.lang.reflect.Method.getDefaultValue(Method.java:720)
    at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:99)
    at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:66)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:202)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:69)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:52)
    at java.lang.reflect.Field.declaredAnnotations(Field.java:1014)
    at java.lang.reflect.Field.getAnnotation(Field.java:1000)
    at java.lang.reflect.AccessibleObject.isAnnotationPresent(AccessibleObject.java:168)
    at org.powermock.reflect.internal.matcherstrategies.FieldAnnotationMatcherStrategy.matches(FieldAnnotationMatcherStrategy.java:37)
    at org.powermock.reflect.internal.WhiteboxImpl.findAllFieldsUsingStrategy(WhiteboxImpl.java:535)
    at org.powermock.reflect.internal.WhiteboxImpl.getFieldsAnnotatedWith(WhiteboxImpl.java:2343)
    at org.powermock.reflect.Whitebox.getFieldsAnnotatedWith(Whitebox.java:585)
    at org.powermock.api.extension.listener.AnnotationEnabler.standardInject(AnnotationEnabler.java:70)
    at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:52)
    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 org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:2014)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:744)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:415)
    at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.injectMocksUsingAnnotationEnabler(PowerMockTestNGMethodHandler.java:72)
    at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.invoke(PowerMockTestNGMethodHandler.java:47)
    at com.actifio.psrv.util.analysis.TestTest_$$_javassist_0.init(TestTest_$$_javassist_0.java)
    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 org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.TestRunner.beforeRun(TestRunner.java:641)
    at org.testng.TestRunner.run(TestRunner.java:609)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
    at org.testng.TestNG.run(TestNG.java:1031)
    at org.testng.TestNG.privateMain(TestNG.java:1338)
    at org.testng.TestNG.main(TestNG.java:1307)
Caused by: java.lang.NullPointerException
    at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
    ... 67 more

I originally thought it was a mockito issue but the code works fine if I remove 
powermock from the test.

I am using:

Sun Java 1.6
testng 6.8
mockito 1.9.5
powermock 1.5

This same code works with:

Sun Java 1.6
testng 6.4
mockito 1.9.0
powermock 1.4.11

Original issue reported on code.google.com by jon.cho...@actifio.com on 4 Dec 2012 at 7:16

GoogleCodeExporter commented 9 years ago
Try extending from PowerMockTestCase and see if you get the same error.

Original comment by johan.ha...@gmail.com on 5 Dec 2012 at 8:59

GoogleCodeExporter commented 9 years ago
Same (similar) issue here with PowerMock 1.5. See attached pom, test case and 
class under test.

Can confirm the problem disappears if I make the test case extend 
PowerMockTestCase.

Original comment by joelcap...@gmail.com on 6 Jan 2013 at 7:38

Attachments:

GoogleCodeExporter commented 9 years ago
I think I have a simpler test case. This seems to happen when a mock is created 
at instantation time. If I set up my mocks in an @Before method the issue goes 
away.

I believe this is a ClassLoader issue - PowerMockMaker is loaded by the system 
classloader, but org.mockito.plugins.MockMaker is also loaded by the 
org.powermock.core.classloader.MockClassLoader and so PowerMockMaker is not an 
instance of MockMaker.

It may be an unavoidable limitation of PowerMock, but the resulting exception 
is completely unhelpful and gives no hint of how to fix the issue.

Simpler example test exhibiting the symptoms:

import java.util.List;

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.junit.Assert.assertFalse;
import static org.mockito.Mockito.mock;

@RunWith(PowerMockRunner.class)
@PrepareForTest(SomeClass.class)
public class TestSomeClass {

    private final List list = mock(List.class);

    @Test
    public void invertTrue() {
        assertFalse(false);
    }
}

Original comment by mahoney...@gmail.com on 24 Feb 2013 at 9:34

GoogleCodeExporter commented 9 years ago
Thanks for your example. People have run into simliar issues when using mock 
policies with PowerMock and Mockito 1.9.5. I've committed a work-around for 
that but I'm not sure if it's as easy in this case.

Original comment by johan.ha...@gmail.com on 25 Feb 2013 at 7:17

GoogleCodeExporter commented 9 years ago
Any process here? Having the same issue, which is very annoying because we 
would like to upgrade to 1.9.5 to get Java 7 support. We can confirm that this 
seems to be a ClassLoading issue.

Original comment by apw.desi...@gmail.com on 17 Apr 2013 at 10:53

GoogleCodeExporter commented 9 years ago
I am facing the same issue using Mockito + powerMock with TestNG. I can't use 
@RunWith since it is a JUnit annotation ; I use @ObjectFactory instead.

Here is my test class :

import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockObjectFactory;
import org.testng.Assert;
import org.testng.IObjectFactory;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;

@PrepareForTest(Static.class)
public class FakeForTestTest {

    @ObjectFactory
    public IObjectFactory setObjectFactory() {
        return new PowerMockObjectFactory();
    }

    @Test
    public void shouldBeValid() {
        PowerMockito.mockStatic(Static.class);
        MockitoAnnotations.initMocks(this);
        Object testObject = new Double(50);
        PowerMockito.when(Static.isValid(testObject)).thenReturn(true);

        FakeForTest fft = new FakeForTest();
        Boolean reel = fft.isValid(testObject);
        Assert.assertTrue(reel, "Error");
    }
}

Original comment by nfr...@gmail.com on 23 Sep 2013 at 5:04

GoogleCodeExporter commented 9 years ago
Need help for this . using java 1.7 

Original comment by hamish.t...@gmail.com on 27 Sep 2013 at 1:27

GoogleCodeExporter commented 9 years ago
When using PowerMock with TestNG always extend your test from 
org.powermock.modules.testng.PowerMockTestCase. By doing this you don't have to 
bother about providing custom object factory, so you will not have 
@ObjectFactory in your test at all. For me this solved the problem.

Original comment by AZagorne...@gmail.com on 27 Sep 2013 at 6:26

GoogleCodeExporter commented 9 years ago
Any workarounds for JUnit? I don't have TestNG nor any of the Powermock TestNG 
libraries on the classpath. 

I've also noticed this issue comes up with @BeforeClass mock initialization.

Original comment by robert....@gmail.com on 9 Oct 2013 at 5:48

GoogleCodeExporter commented 9 years ago
Same issue here with Junit.
Workaround is to add "org.mockito.*" in @PowerMockIgnore

Original comment by arnaud.l...@gmail.com on 28 Oct 2013 at 3:52

GoogleCodeExporter commented 9 years ago
Looks like this same issue was fixed in gwtMockito, perhaps that can help here 
as well?

https://github.com/google/gwtmockito/issues/14

Original comment by hashbro...@gmail.com on 1 Nov 2013 at 4:09

GoogleCodeExporter commented 9 years ago
Based on the fix mentioned by in #11, I created a patch for 
JUnit4TestSuiteChunkerImpl that seems to fix the problem. I ran it on a project 
internally w/ ~700 tests that used to fail due to this bug, and they now 
succeed.

I also attempted to figure out how to add a test case to powermock for this 
issue, but it turned out to be easier to find the location for the fix than it 
was to find out where I would put a test for it. :)

Patch file is attached.

Original comment by robert....@redfin.com on 13 Nov 2013 at 6:20

Attachments:

GoogleCodeExporter commented 9 years ago
Sorry, I should have been more clear: I created a patch for the issue when 
running through JUnit. I don't have much/any experience w/ TestNG. 

Original comment by robert....@redfin.com on 13 Nov 2013 at 6:22

GoogleCodeExporter commented 9 years ago
I have a similar issue. 
My platform is Testng-6.8.7 and Powermock-1.5.2, and Mockito-1.9.5
And if I extends PowerMockTestCase, then the issue is not happening. Only when 
I use the following. 

@ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new org.powermock.modules.testng.PowerMockObjectFactory();
    }

The following is the exceptions error. 

java.lang.ExceptionInInitializerError
    at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
    at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30)
    at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:19)
    at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
    at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:145)
    at org.mockito.internal.configuration.ClassPathLoader.findPluginImplementation(ClassPathLoader.java:110)
    at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:106)
    at org.mockito.internal.configuration.ClassPathLoader.<clinit>(ClassPathLoader.java:59)
    at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:21)
    at org.mockito.internal.configuration.injection.scanner.MockScanner.<init>(MockScanner.java:22)
    at org.mockito.internal.configuration.InjectingAnnotationEngine.injectMocks(InjectingAnnotationEngine.java:96)
    at org.powermock.api.mockito.internal.configuration.PowerMockitoInjectingAnnotationEngine.process(PowerMockitoInjectingAnnotationEngine.java:35)
    at org.powermock.api.extension.listener.AnnotationEnabler.injectSpiesAndInjectToSetters(AnnotationEnabler.java:57)
    at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:52)
    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:606)
    at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:2014)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:744)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:415)
    at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.injectMocksUsingAnnotationEnabler(PowerMockTestNGMethodHandler.java:75)
    at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.invoke(PowerMockTestNGMethodHandler.java:47)
    at com.ibaset.solumina.sfcore.application.impl.LoginImplTest_$$_jvstdd8_0.getFoundationVersion_NoDash_LongSameShortEmpty(LoginImplTest_$$_jvstdd8_0.java)
    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:606)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.NullPointerException
    at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
    ... 48 more

Original comment by Jaepyoun...@gmail.com on 9 Jan 2014 at 9:16

GoogleCodeExporter commented 9 years ago
Following 2 workarounds helped me get over this:
1. Do not use private static instance field:
   private static final MockMe ME = new MockMe();

2. Make TestNG test method 'final'.  Not sure how this helps, but it did help 
me.

Original comment by khid...@gmail.com on 24 Sep 2014 at 6:58