Recently at work a big amount of effort has been put in to adding unit tests for as much code as possible. Due to the nature of the services that we work with, we required to make heavy use of PowerMock
. It's a great framework that lets us mock out certain classes that are usually hard to mock, like statics, and unfortunately at one point I started to see LinkageError
exceptions being thrown that looked like this...
So what does this mean? Well PowerMock
prepares and loads mocked classes using its MockClassLoader
. In some cases this behaviour is undesirable and the mocked class must be loaded by the system class loader instead. This is easily done with the PowerMockIgnore
annotation like this...
So in my case since the LinkageError
was for the javax/management/MBeanServer
class, I use the "javax.management.*"
value to make sure that everything in the javax.management
package is deferred to the system class loader (I could have used the specific class but I chose to use the entire package).
There is a way to apply this type of configuration globally by creating a file, "org/powermock/extensions/configuration.properties"
, and adding it to the classpath (if using Maven in the src/test/resources
directory). The configuration file is a standard Java Properties file and needs to have the "powermock.global-ignore"
set the same way as the annotation, however I couldn't get this to work.
Hope you found this post useful...
...so please read on! I love writing articles that provide beneficial information,
tips and examples to my readers. All information on my blog is provided free of
charge and I encourage you to share it as you wish. There is a small favour I ask in return however -
engage in comments below, provide feedback, and if you see mistakes let me know.
If you want to show additional support and help me pay for web hosting and
domain name registration,
donations, no matter how small, are always welcome!
Use of any information contained in this blog post/article is subject to this disclaimer
Other posts you may like...