tiebin-zhang / powermock

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

PowerMock (API mockito) - Slf4j (API logbak) #358

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
HI,

 here are the details of my issue :

What steps will reproduce the problem?
1. Try to configure slf4j dynamically

       LoggerContext context = (LoggerContext) LoggerFactory
                .getILoggerFactory();
        context.reset();
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(context);
        URL url = new URL("file:/"
                + System.getProperty("logback.configurationFile"));
        configurator.doConfigure(url);

2. Launch the unit test where log configuration is called in BeforeClass method.

3. Exception is thrown :

Failed to auto configure default logger context
Reported exception:
ch.qos.logback.core.joran.spi.JoranException: Parser configuration error 
occurred
    at ch.qos.logback.core.joran.event.SaxEventRecorder.buildSaxParser(SaxEventRecorder.java:86)
    at ch.qos.logback.core.joran.event.SaxEventRecorder.recordEvents(SaxEventRecorder.java:57)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:132)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:96)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:55)
    at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:75)
    at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:148)
    at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84)
    at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:54)
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:128)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:279)
    at fr.canalplus.bigbro.test.utils.AbstractBigBroTest.initLogs(AbstractBigBroTest.java:101)
    at fr.canalplus.bigbro.test.utils.AbstractBigBroTest.beforeClass(AbstractBigBroTest.java:68)
    at fr.canalplus.bigbro.cache.CacheRetryTest.init(CacheRetryTest.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.internal.runners.ClassRoadie.runBefores(ClassRoadie.java:56)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:43)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    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)
Caused by: java.lang.ClassCastException: 
com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl cannot be cast to 
javax.xml.parsers.SAXParserFactory
    at javax.xml.parsers.SAXParserFactory.newInstance(Unknown Source)
    at ch.qos.logback.core.joran.event.SaxEventRecorder.buildSaxParser(SaxEventRecorder.java:79)

What is the expected output ?
Test runs OK

What version of the product are you using? On what operating system?
powermock-api-mockito 1.4.9 (on Windows XP)

I've also tried to use the MockPolicy with the class provided in another post : 
@MockPolicy(Sfl4jMockPolicy.class)
--> Same result

I've tried to ignore packages from slf4j and/or logbak, and then another 
exception is thrown :

java.lang.ClassCastException: 
org.slf4j.Logger$$EnhancerByMockitoWithCGLIB$$c3ce6cd8 cannot be cast to 
org.mockito.cglib.proxy.Factory
    at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy(ClassImposterizer.java:111)
    at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:51)
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:54)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:45)
    at org.mockito.Mockito.mock(Mockito.java:921)
    at org.mockito.Mockito.mock(Mockito.java:816)
    at fr.canalplus.bigbro.test.utils.Sfl4jMockPolicy.initializeMockForThread(Sfl4jMockPolicy.java:82)
    at fr.canalplus.bigbro.test.utils.Sfl4jMockPolicy.applyInterceptionPolicy(Sfl4jMockPolicy.java:70)
    at org.powermock.tests.utils.impl.MockPolicyInitializerImpl.getInterceptionSettings(MockPolicyInitializerImpl.java:189)
    at org.powermock.tests.utils.impl.MockPolicyInitializerImpl.initializeInterceptionSettings(MockPolicyInitializerImpl.java:161)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:2014)
    at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:885)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:713)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
    at org.powermock.tests.utils.impl.MockPolicyInitializerImpl.invokeInitializeInterceptionSettingsFromClassLoader(MockPolicyInitializerImpl.java:138)
    at org.powermock.tests.utils.impl.MockPolicyInitializerImpl.initialize(MockPolicyInitializerImpl.java:124)
    at org.powermock.tests.utils.impl.MockPolicyInitializerImpl.initialize(MockPolicyInitializerImpl.java:90)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createNewClassloader(AbstractTestSuiteChunkerImpl.java:205)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.chunkClass(AbstractTestSuiteChunkerImpl.java:170)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.<init>(AbstractTestSuiteChunkerImpl.java:92)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.<init>(AbstractTestSuiteChunkerImpl.java:85)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:47)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
    at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:26)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    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)

Thanks in advance
Matthieu

Original issue reported on code.google.com by yoda...@gmail.com on 1 Dec 2011 at 12:57

GoogleCodeExporter commented 9 years ago
I found a solution, don't know if it's a clean one but it works for me :

@PowerMockIgnore({ "javax.management.*", "javax.xml.parsers.*",
        "com.sun.org.apache.xerces.internal.jaxp.*", "ch.qos.logback.*",
        "org.slf4j.*" })

Mat

Original comment by yoda...@gmail.com on 1 Dec 2011 at 5:49

GoogleCodeExporter commented 9 years ago
Hi, 

Sorry to say but unfortunately you have to resort to ugly fixes like that in 
some cases. How ever you can extract all of the ignored packages to a reusable 
mock policy (see documentation) so that you don't have to repeat it all over 
the place. 

Another solution would be to use the PowerMock javaagent (see docs again) but 
it's highly experimental and may not work in all situations.

/Johan

Original comment by johan.ha...@gmail.com on 4 Dec 2011 at 9:33

GoogleCodeExporter commented 9 years ago
Hi,
iam also getting the same error like,
java.lang.ClassCastException: 
com.org.test.ITestInterface$$EnhancerByMockitoWithCGLIB$$c3ce6cd8 cannot be 
cast to org.mockito.cglib.proxy.Factory

could anyone give the solution for the above.

Thanks in advance 
Vanisree

Original comment by mvanisre...@gmail.com on 8 Mar 2012 at 4:33

GoogleCodeExporter commented 9 years ago
@Matthieu the detailed description is great, i did encounter the same issue. 
And your solution also fix my issue. 

One pain point is that i have to run the test a couple times to see how many 
packages need to be in my @PowerMockIgnore annotation which make it more like a 
workaround but not a solution..

Original comment by suliang...@gmail.com on 12 Nov 2013 at 5:56