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
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With