log4j:ERROR A “org.apache.log4j.ConsoleAppender” o

2019-04-04 08:52发布

问题:

I am making a web app using gradle. I used log4j in this app. When I build the project I am getting 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@16a40b2] whereas object of type
  • log4j:ERROR "org.apache.log4j.ConsoleAppender" was loaded by [sun.misc.Launcher$AppClassLoader@5e3a78ad].
  • log4j:ERROR Could not instantiate appender named "stdout".

I still did not find a good answer. And I forgot to say, My project build correctly and show log massages correctly. It doesn't work only on test classes.

回答1:

Adding this annotation works for me.

@PowerMockIgnore("org.apache.log4j.*") 


回答2:

Your classpath contains 2 copies of log4j library. Exclude one and try again.



回答3:

There is a Clear answer here. :D

When we have a final method and slf4j loggers in our classes. We have to use powermock in our test class. Because of this we have to use @MockPolicy(Slf4jMockPolicy.class) with particuler imports at the test class which we used Powermock.