13

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

abhig
  • 840
  • 2
  • 12
  • 22
  • How did you find this "solution"? Because it's look more like workaround than solution... – Line May 19 '17 at 07:11
  • @Line Nopes :( . In ideal case, we should never need to use PowerMock if we write our code testable – abhig May 20 '17 at 19:22
  • But what "nopes"? Can you tell me where this @PowerIgnore comes from, how did you found out about it? – Line May 22 '17 at 07:12

2 Answers2

12

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

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
Artur Zagretdinov
  • 2,034
  • 13
  • 22
  • 6
    When used in properties file there should be no double quotes i.e. powermock.global-ignore="org.myproject.*" -> powermock.global-ignore=org.myproject.* – user3474985 Jan 03 '19 at 23:35
  • @user3474985, `+1` – Michał Ziober Jun 24 '20 at 19:05
  • To add to this, if you want to add more classes to ignore on one of your tests, you can still add this into the `@PowerMockIgnore(value = {org.class.*})` annotation. If you want the opposite and to ignore the global setting, use `@PowerMockIgnore(globalIgnore = false)` – Navigatron Sep 27 '21 at 17:08
  • Note, spaces and double quotes are not allowed in the configuration.properties file. See: https://github.com/powermock/powermock/issues/989 – Navigatron Sep 28 '21 at 08:39
5

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!

GhostCat
  • 137,827
  • 25
  • 176
  • 248