Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@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

like image 846
abhig Avatar asked Jan 12 '17 00:01

abhig


2 Answers

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!

like image 119
GhostCat Avatar answered Nov 08 '22 12:11

GhostCat


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

like image 35
Arthur Zagretdinov Avatar answered Nov 08 '22 13:11

Arthur Zagretdinov