Re: [powermock] SLF4J issue with powermock

1,387 views
Skip to first unread message

Johan Haleby

unread,
Feb 4, 2013, 9:26:32 AM2/4/13
to powe...@googlegroups.com
You can try using a MockPolicy, for example the Slf4jMockPolicy.

Regards,
/Johan

On Mon, Feb 4, 2013 at 10:20 AM, Ratheesh Nair <rathee...@gmail.com> wrote:
Hi ,
I have a  method that use SLF4J api's(here we use logback as the SLF4J implementation) for logging. While execution unit test on those method I'm getting java.lang.LinkageError: loader constraint violation.

java.lang.LinkageError: loader constraint violation: when resolving method "ch.qos.logback.classic.spi.LoggingEvent.<init>(Ljava/lang/String;Lch/qos/logback/classic/Logger;Lch/qos/logback/classic/Level;Ljava/lang/String;Ljava/lang/Throwable;[Ljava/lang/Object;)V" the class loader (instance of org/powermock/core/classloader/MockClassLoader) of the current class, ch/qos/logback/classic/Logger, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for resolved class, ch/qos/logback/classic/spi/LoggingEvent, have different Class objects for the type ch/qos/logback/classic/Logger used in the signature
at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:447)
at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:403)

I have verified for the dependecy conflict in the class path and I didn't find any issue. Also this issue occur only some time.

Please let me know any additional configurtion needed to add for power mock to solve this issue

--
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.
 
 

Ratheesh Nair

unread,
Feb 11, 2013, 4:51:39 AM2/11/13
to powe...@googlegroups.com
Thanks Johan, Its working.

But after using the mockpolicy I'm getting class cast exception, we are using testng and using @ObjectFactory anotation
Our unit test class have a slf4j logger class member to just log the test details. This issue occur some times only

error details

Caused by: java.lang.ClassCastException: org.slf4j.Logger$$EnhancerByMockitoWithCGLIB$$79f3203a 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:52)
        at org.mockito.internal.MockitoCore.mock(MockitoCore.java:41)
        at org.mockito.Mockito.mock(Mockito.java:1014)
        at org.mockito.Mockito.mock(Mockito.java:909)
        at org.powermock.api.mockito.mockpolicies.Slf4jMockPolicy.initializeMockForThread(Slf4jMockPolicy.java:71)
        at org.powermock.api.mockito.mockpolicies.Slf4jMockPolicy.applyInterceptionPolicy(Slf4jMockPolicy.java:60)
        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(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.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.modules.testng.internal.PowerMockClassloaderObjectFactory.newInstance(PowerMockClassloaderObjectFactory.java:75)
        at org.powermock.modules.testng.PowerMockObjectFactory.newInstance(PowerMockObjectFactory.java:42)
        at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
        ... 28 more

Johan Haleby

unread,
Feb 11, 2013, 6:39:40 AM2/11/13
to powe...@googlegroups.com
Hm it looks to me like it might be a dependency conflict. I cannot give you any help on this unfortunately unless you can reproduce it in a test case that you can supply to us.

/Johan

Ratheesh Nair

unread,
Feb 11, 2013, 6:55:15 AM2/11/13
to powe...@googlegroups.com
Here is the full stacktrace. I just checked any dependecy conflit for Slf4j and I didn't find any issue

Running TestSuite
org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException; nested exception is java.lang.reflect.InvocationTargetException: null
java.lang.reflect.InvocationTargetException
        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.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
        at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:172)
        at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:104)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:70)
Caused by: org.testng.TestNGException:
An error occurred while instantiating class com.testApp.cpc.apps.pcshen.dect.SimpleTest: org.slf4j.Logger$$EnhancerByMockitoWithCGLIB$$1c6d013e cannot be cast to org.mockito.cglib.proxy.Factory
        at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:398)
        at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
        at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
        at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:195)
        at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
        at org.testng.TestRunner.initMethods(TestRunner.java:409)
        at org.testng.TestRunner.init(TestRunner.java:235)
        at org.testng.TestRunner.init(TestRunner.java:205)
        at org.testng.TestRunner.<init>(TestRunner.java:153)
        at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:522)
        at org.testng.SuiteRunner.init(SuiteRunner.java:157)
        at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
        at org.testng.TestNG.createSuiteRunner(TestNG.java:1272)
        at org.testng.TestNG.createSuiteRunners(TestNG.java:1259)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1113)
        at org.testng.TestNG.run(TestNG.java:1030)
        at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:70)
        at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:157)
        at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:97)
        at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:111)
        ... 9 more
Caused by: java.lang.ClassCastException: org.slf4j.Logger$$EnhancerByMockitoWithCGLIB$$1c6d013e cannot be cast to org.mockito.cglib.proxy.Factory

Johan Haleby

unread,
Feb 11, 2013, 7:13:30 AM2/11/13
to powe...@googlegroups.com
What I mean is a full test case and code that demonstrates the issues. 

/Johan
Message has been deleted

Johan Haleby

unread,
Jun 13, 2013, 2:01:44 AM6/13/13
to powe...@googlegroups.com
Thanks, I'll try to look into it when I find some time. Also could you do me a favor and try if you still have the same problems with the trunk version of PowerMock?


On Mon, Jun 10, 2013 at 3:40 PM, Ola Christian Gundelsby <ola-christi...@telenor.com> wrote:
Hi Johan

Here's a maven project with a test case that fails. This is easymock rather than mockito, but the error seems to be the same.
This fails with the same stacktrace with slf4j version 1.6.4 and 1.7.5, and with PowerMock versions 1.4.12 and 1.5 alike.
Tested with the internal JUnit plugin in Eclipse and with mvn test.


Hope this helps.


-ola
Message has been deleted

Johan Haleby

unread,
Jun 16, 2013, 2:14:47 AM6/16/13
to powe...@googlegroups.com
I think you're not getting the same error as the others but another one. You get an error because you don't have an slf4j implementation in classpath, you only have the API. If you remove the slf4j mock policy you end up with:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation

slf4j-api is an SPI which depends on an implementation so PowerMock fails to byte-code manipulate slf4j since one of it's methods returns org.slf4j.impl.StaticLoggerBinder which cannot be found in classpath. How ever I've tweaked the byte-code manipulation in PowerMock to ignore modifying these methods. I've committed the changes to trunk so the test pass if you use PowerMock from trunk.

Regards,
/Johan



On Mon, Jun 10, 2013 at 3:40 PM, Ola Christian Gundelsby <ola-christi...@telenor.com> wrote:
Hi Johan

Here's a maven project with a test case that fails. This is easymock rather than mockito, but the error seems to be the same.
This fails with the same stacktrace with slf4j version 1.6.4 and 1.7.5, and with PowerMock versions 1.4.12 and 1.5 alike.
Tested with the internal JUnit plugin in Eclipse and with mvn test.


Hope this helps.


-ola


kl. 13:13:30 UTC+1 mandag 11. februar 2013 skrev Johan Haleby følgende:

Ola Christian Gundelsby

unread,
Jun 17, 2013, 6:09:07 AM6/17/13
to powe...@googlegroups.com
Yup, the test passes now.
Thanks!


-ola
Reply all
Reply to author
Forward
0 new messages