Skip to content

PowerMock Configuration

Arthur Zagretdinov edited this page Apr 21, 2019 · 5 revisions

PowerMock Configuration

   Feature avaliable since PowerMock 1.7.0

As of version of 1.7.0 it's possible to globally configure PowerMock. 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

Global @PowerMockIgnore

By default PowerMock loads all classes with its MockClassLoader. The classloader loads and modified all classes except:

  • system classes. They are deferred to system classloader
  • classes located in packages that are specified as ignored.

Before 1.7.0 the only one way to ignore a package was to use the @PowerMockIgnore annotation:

 @PowerMockIgnore("org.myproject.*")
 @PrepareForTest(MyClass.class)
 @RunWith(PowerMockRunner.class)
 public class MyTest {
 ...
 }

In some cases the only way to avoid certain classloading related issues is to use @PowerMockIgnore and this used to imply that @PowerMockIgnore was copied and pasted in many tests.

But 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"

Example of using PowerMock Configuration for global @PowerMockIgnore.

Mockito mock-maker-inline

PowerMock can delegate calls to another MockMaker, these tests are run without PowerMock. This can be configured by adding the following property to configuration file:

mockito.mock-maker-class=mock-maker-inline

Example of using Mockito mock-maker-inline with PowerMock