Questions tagged [powermock]

Use this tag for questions about PowerMock, a Java library for creating mock objects for classes and methods. Questions about PowerMock's extension for Mockito should be tagged [powermockito] instead.

PowerMock is a Java unit testing library that takes mock creation further than is possible using Mockito and EasyMock and facilitates mock creation and behaviour in areas that they deem un-testable.

Facilities available from PowerMock include:

  • mocking static, private and final methods
  • partial mocking, and
  • mocking construction of new objects

The project is open source on Apache License 2.0 and is available in Git Hub.

2039 questions
51
votes
4 answers

Junit Parameterized tests together with Powermock - how?

I've been trying to figure out how to run parameterized tests in Junit4 together with PowerMock. The problem is that to use PowerMock you need to decorate your test class with @RunWith(PowerMockRunner.class) and to use parameterized tests you have…
Anders Hansson
  • 3,746
  • 3
  • 28
  • 27
46
votes
2 answers

PowerMock, mock a static method, THEN call real methods on all other statics

I'm setting up mocking a class' static methods. I have to do this in a @Before-annotated JUnit setup method. My goal is to setup the class to call real methods, except for those methods I explicitly mock. Basically: @Before public void…
Tom Tresansky
  • 19,364
  • 17
  • 93
  • 129
45
votes
7 answers

cannot resolve symbol PowerMockRunner

I'm trying to use Powermock for the first time I use build.gradle and added: dependencies { ... testCompile 'org.mockito:mockito-all:1.9.5' testCompile 'org.powermock:powermock-api-mockito:1.5.5' } now I look at my test class which…
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
44
votes
4 answers

Mocking extension function in Kotlin

How to mock Kotlin extension function using Mockito or PowerMock in tests? Since they are resolved statically should they be tested as static method calls or as non static?
Romper
  • 2,009
  • 3
  • 24
  • 43
44
votes
7 answers

What do I use instead of Whitebox in Mockito 2.2 to set fields?

When using Mockito 1.9.x I have been using Whiteboxto set values of fields to "inject" mocks. Se example below: @Before public void setUp() { eventHandler = new ProcessEventHandler(); securityService = new SecurityServiceMock(); …
emanciperingsivraren
  • 1,215
  • 2
  • 15
  • 27
41
votes
5 answers

NoClassDefFoundError when using Powermock

I'm running a junit test case using the PowerMock test runner. I'm using the following command line to execute it: java -cp .:junit-4.9b2.jar:easymock-3.0.jar:powermock-easymock-1.4.8-full.jar org.junit.runner.JUnitCore SampleTest When doing so I…
lukas
  • 421
  • 1
  • 4
  • 4
41
votes
8 answers

Powermock - java.lang.IllegalStateException: Failed to transform class

Description: I am trying to test a static method from a class. I am using powerMock (1.6.2) + mockito (1.10.19) for mocking along with Junit4 (4.12) & java8. Issue: Getting the error: "Failed to transform class with name…
Lucky1989
  • 411
  • 1
  • 4
  • 5
41
votes
12 answers

Unable to get Jacoco to work with Powermockito using offline instrumentation

Given that Jacoco doesn't play nicely with PowerMockito when instrumenting "on the fly", I've been trying to configure offline instrumentation in the hope this will give me proper unit test coverage for classes that use PowerMockito. I've setup my…
MandyW
  • 1,117
  • 3
  • 14
  • 23
39
votes
12 answers

Mockito asks to add @PrepareForTest for the class even after adding @PrepareForTest

I have the following simple code. I have a class (TestClass) and I want to test "someMethod". There is an external static method which is called by my "someMethod". I want to Powermock that static method to return me some dummy object. I have the…
ViV
  • 1,998
  • 8
  • 27
  • 54
37
votes
6 answers

Junit5 mock a static method

I want to mock a static method in JUnit 5. But unfortunately, JUnit 5 doesn’t support Mockito. Is there another method to achieve the same other than reverting back to JUnit 4?
Priya
  • 1,096
  • 4
  • 15
  • 32
36
votes
3 answers

How to mock a void static method to throw exception with Powermock?

I am trying to use Powermock and Mockito to mock a void static method to throw exception as below. But I met a problem. Unless I make the two invocations of Adder.add() with the same argument, the mocked IOException won't be thrown. BTW, I've added…
Smartmarkey
  • 1,979
  • 5
  • 22
  • 25
35
votes
5 answers

MockClassLoader cannot access jdk/internal/reflect superclass jdk.internal.reflect.MagicAccessorImpl

I am in the middle of migrating a project into Java9, The Tests start failing after I switched to the new Java version, it seems like PowerMock is trying to access some classes it does not have access to. Tests run: 1, Failures: 0, Errors: 1,…
Arar
  • 1,926
  • 5
  • 29
  • 47
34
votes
1 answer

What does @PrepareForTest in PowerMock really mean?

What does the annotation @PrepareForTest in PowerMockito really mean? What should be placed there apart of classes which have static methods?
Ernesto
  • 950
  • 1
  • 14
  • 31
34
votes
4 answers

after upgrade to 2.7 ClassNotFoundException: org.mockito.exceptions.Reporter when run test

I try to set up junit, mokito and powermock together but when I ran a test I get ClassNotFoundException :( testCompile 'junit:junit:4.12' testCompile 'org.mockito:mockito-core:2.7.22' androidTestCompile…
LunaVulpo
  • 3,043
  • 5
  • 30
  • 58
33
votes
1 answer

Powermock and Spring cause ConversionException when injecting EntityManager in test

When attempting to combine the Spring test runner and the PowerMock runner using the PowerMockRule technique, I get an exception from the Thoughtworks XStream library whenever I try to inject an EntityManager using the PersistenceContext annotation…
amoe
  • 4,473
  • 4
  • 31
  • 51