Re: [powermock] Logging basics with Powermock and Mockito

1,794 views
Skip to first unread message

Johan Haleby

unread,
Feb 20, 2013, 5:09:54 AM2/20/13
to powe...@googlegroups.com
What version of PowerMock and Mockito are you using?

Regards,
/Johan

On Wed, Feb 20, 2013 at 10:32 AM, Joachim <joachimvan...@gmail.com> wrote:
Hi, I am new to mocking. Could anyone explain how you can use a logger in a test case with powermock and mockito?
The idea is to just output messages to the console while doing a unit test. I tried the following, but I get an ExceptionInInitialzerError.

Would it also be possible to explain why the code below does not work?

Thx,
Joachim


Test class:


import org.junit.runner.RunWith;
import org.powermock.api.mockito.mockpolicies.Slf4jMockPolicy;
import org.powermock.core.classloader.annotations.MockPolicy;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RunWith(PowerMockRunner.class)
@MockPolicy(Slf4jMockPolicy.class)
@PrepareForTest({Sandbox.class})

public class TestSandbox {
  static final Logger LOGGER = LoggerFactory.getLogger(TestSandbox.class);
}

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.MockitoCore.<init>(MockitoCore.java:40)
at org.mockito.Mockito.<clinit>(Mockito.java:932)
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:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
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:33)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:33)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
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.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
at sun.proxy.$Proxy0.invoke(Unknown Source)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)

Caused by: java.lang.NullPointerException
at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
... 55 more

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

Joachim

unread,
Feb 20, 2013, 5:49:42 AM2/20/13
to powe...@googlegroups.com

Here are the versions I am using.
Thx!

<properties>
  <org.springframework.version>3.0.0.RELEASE</org.springframework.version>
  <org.aspectj.version>1.5.4</org.aspectj.version>
  <org.powermock.version>1.5</org.powermock.version>
  <org.mockito.version>1.9.5</org.mockito.version>
  <org.slf4j.version>1.6.2</org.slf4j.version>
  <org.apache.log4j.version>1.2.17</org.apache.log4j.version>
</properties>

Johan Haleby

unread,
Feb 20, 2013, 6:07:18 AM2/20/13
to powe...@googlegroups.com
Hmm could you provide a small example project (preferably maven) that demonstrates this issue?

Regards,
/Johan

--

Joachim

unread,
Feb 20, 2013, 6:32:53 AM2/20/13
to powe...@googlegroups.com
I uploaded a small project, but I noticed that the error is probably related to the PowerMockRunner and not to the logging configuration:
I left out the declaration of the Logger and I still get the initialization error.

Regards,
Joachim
powermock-mockito-logging-problem.zip

Johan Haleby

unread,
Feb 20, 2013, 7:29:26 AM2/20/13
to powe...@googlegroups.com
Thanks, I've reproduced it but I don't know why this is happening. It can be tricky to fix.

--

Johan Haleby

unread,
Feb 20, 2013, 7:40:46 AM2/20/13
to powe...@googlegroups.com
Ok I think the problem is that Mockito in its initialization tries to load the "PowerMockMaker" from the wrong classloader. I'll have to dig in and see if there's something we can do about this...

/Johan

Johan Haleby

unread,
Feb 20, 2013, 7:59:20 AM2/20/13
to powe...@googlegroups.com
I think I've fixed it in trunk now. Please try to build PowerMock from the source (mvn clean install using Java 6) and try if the fix works for you. 

Regards,
/Johan

Joachim

unread,
Feb 21, 2013, 6:01:49 AM2/21/13
to powe...@googlegroups.com
I built the maven project (in Java 7). 

Looking at the dependency graph,
I included the following dependencies explicitely, so I had the correct (newly built) versions

powermock-core
powermock-module-junit4-common
powermock-module-junit4

powermock-api-mockito
powermock-api-support

I noticed I had to explicitely include

powermock-reflect

When not included, I get the following error

java.lang.NoClassDefFoundError: org/powermock/reflect/Whitebox
at org.powermock.tests.utils.impl.MockPolicyInitializerImpl.<init>(MockPolicyInitializerImpl.java:67)
at org.powermock.tests.utils.impl.MockPolicyInitializerImpl.<init>(MockPolicyInitializerImpl.java:52)
at org.powermock.tests.utils.impl.MockPolicyInitializerImpl.<init>(MockPolicyInitializerImpl.java:44)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.hasMockPolicyProvidedClasses(AbstractTestSuiteChunkerImpl.java:386)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createNewClassloader(AbstractTestSuiteChunkerImpl.java:192)

Caused by: java.lang.ClassNotFoundException: org.powermock.reflect.Whitebox

at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 33 more

Check to see if the build path contains all new jars:

[DEBUG]   /Users/joachim/.m2/repository/org/powermock/powermock-core/1.5.1/powermock-core-1.5.1.jar
[DEBUG]   /Users/joachim/.m2/repository/org/powermock/powermock-module-junit4/1.5.1/powermock-module-junit4-1.5.1.jar
[DEBUG]   /Users/joachim/.m2/repository/org/powermock/powermock-module-junit4-common/1.5.1/powermock-module-junit4-common-1.5.1.jar
[DEBUG]   /Users/joachim/.m2/repository/org/powermock/powermock-api-support/1.5.1/powermock-api-support-1.5.1.jar
[DEBUG]   /Users/joachim/.m2/repository/org/powermock/powermock-api-mockito/1.5.1/powermock-api-mockito-1.5.1.jar
[DEBUG]   /Users/joachim/.m2/repository/org/powermock/powermock-reflect/1.5.1/powermock-reflect-1.5.1.jar

I now get the following error:

java.lang.NoClassDefFoundError: javassist/expr/ExprEditor

at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createNewClassloader(AbstractTestSuiteChunkerImpl.java:196)

Caused by: java.lang.ClassNotFoundException: javassist.expr.ExprEditor

at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 29 more


Joachim

unread,
Feb 21, 2013, 6:03:23 AM2/21/13
to powe...@googlegroups.com
Here's the updated project in a zip


powermock-mockito-logging-problem.zip

Johan Haleby

unread,
Feb 21, 2013, 7:00:19 AM2/21/13
to powe...@googlegroups.com
You need all external dependencies as well otherwise it won't work. The simplest thing is probably to build it using Maven and depend on the snapshot version in your current project using Maven as well. That way all dependencies will be correctly resolved.

/Johan

On Thu, Feb 21, 2013 at 12:03 PM, Joachim <joachimvan...@gmail.com> wrote:
Here's the updated project in a zip

Joachim

unread,
Feb 21, 2013, 10:56:41 AM2/21/13
to powe...@googlegroups.com
I am now rebuilding from within Eclipse, but I get compile errors in a couple of tests because "when" (amongst others) is ambiguous

For example in powermock.modules.test.mockito.junit4.agent.MockStaticTest

@Test
public void testMockStaticWithExpectations() throws Exception {
  final String expected = "Hello world";
  final String argument = "hello";

  mockStatic(StaticService.class);
  when(StaticService.say(argument)).thenReturn(expected);
  assertEquals(expected, StaticService.say(argument));
  // Verification is done in two steps using static methods.
  verifyStatic();
  StaticService.say(argument);
}

Any suggestions?

Johan Haleby

unread,
Feb 21, 2013, 12:31:13 PM2/21/13
to powe...@googlegroups.com
I don't get these kind of errors so I suspect it might be a classpath issue?

Regards,
/Johan

--

Joachim

unread,
Feb 21, 2013, 2:23:08 PM2/21/13
to powe...@googlegroups.com
It was a classpath issue in Eclipse (STS 3.1.0.RELEASE) that I could not resolve. When building in a console I had to skip the tests
with -Dmaven.test.skip=true, after which everything built neatly.

Now the PowerMockRunner issue has been resolved, but the logging still fails with the following error

log4j:ERROR A "org.apache.log4j.ConsoleAppender" object is not assignable to a "org.apache.log4j.Appender" variable.
log4j:ERROR The class "org.apache.log4j.Appender" was loaded by
log4j:ERROR [org.powermock.core.classloader.MockClassLoader@7f2a3793] whereas object of type
log4j:ERROR "org.apache.log4j.ConsoleAppender" was loaded by [sun.misc.Launcher$AppClassLoader@a6eb38a].
log4j:ERROR Could not instantiate appender named "console".
log4j:WARN No appenders could be found for logger (be.inqa.TestLogging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

I resolved it using @PowerMockIgnore("org.apache.log4j.*"), as mentioned on http://code.google.com/p/powermock/wiki/FAQ

Still, this seems strange to me as @MockPolicy(Log4jMockPolicy.class) is recommended in the FAQ document.

Anyway, it works now - thanks for the fix!


Reply all
Reply to author
Forward
0 new messages