@Powermockignore at Project Level

I have following error in my powermock test cases while running in Maven :

java.lang.LinkageError: loader constraint violation: loader (instance of org/powermock/core/classloader/MockClassLoader) previously initiated loading for a different type with name "javax/management/MBeanServer"

The solution is to add annotation

   @PowerMockIgnore("javax.management.*")

The problem is I have many test files where I have to add this annotation.

Is there a way to add this at project level or in maven?

Thanks

3

2 Answers

Since PowerMock 1.7.0 you can specify packages to ignore using the configuration file.

powermock.global-ignore=org.myproject.*

Multiple packages/classes can be specified using comma:

powermock.global-ignore=org.myproject.*,org.3rdpatproject.SomeClass

This configuration is applied to all test classes that in the classpath. You may enable the configuration by creating by adding this file to the classpath:

org/powermock/extensions/configuration.properties

4

I don't think that this is possible.

Keep in mind, in the end, it is JUnit that is executing those testcases. One by one. And JUnit knows nothing about a maven "project" around.

JUnit only knows about the elements within the test class it is about to process.

Thus: those statements need to to go into all your testcases; ideally only in those that really require it.

Final word: please don't get me wrong, but in my opinion your real problem is that you seem to use PowerMock indiscriminately in your project. That could indicate many of your developers dont know how to write testable code ... and then they "fix" that by turning to PowerMock. There is a certain chance that you will regret that sooner or later.

Edit: for learning how to write testable code --- start here!

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest