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
33
votes
4 answers

Is it possible to verify a mock method running in different thread in Mockito?

I have a method like the following, public void generateCSVFile(final Date billingDate) { asyncTaskExecutor.execute(new Runnable() { public void run() { try { accessService.generateCSVFile(billingDate); …
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
32
votes
7 answers

PowerMock & Java 11

We are using PowerMock in few of our historical projects. Unfortunately PowerMock is quite dead and is not compatible with Java 11. And we are using mockStatic(). Yes, we know its considered harmful - its in the legacy code and we would prefer not…
malejpavouk
  • 4,297
  • 6
  • 41
  • 67
31
votes
4 answers

Replacing PowerMock's @PrepareForTest programmatically?

I am using PowerMock to mock static methods in junit tests, typically done as follows: @RunWith(PowerMockRunner.class) @PrepareForTest({Foo.class,Bar.class}) public class SomeUnitTest { @Before public void setUpTest() { setUpFoo(); …
John Q Citizen
  • 3,138
  • 4
  • 26
  • 31
30
votes
5 answers

Mocked private method with PowerMock, but underlying method still gets called

I am trying to mock out a private method that is making a JNDI call. When that method gets called from a unit test, it throws an exception^. I would like to mock-out that method for testing purposes. I used the sample code from another questions…
Sled
  • 18,541
  • 27
  • 119
  • 168
30
votes
3 answers

Mocking method calls using power mockito - org.powermock.api.mockito.ClassNotPreparedException

I have an image loader class and i need to test some static methods in it. Since Mockito does not support static methods i switched to Power Mockito. But the static method i am testing has a method call Base64.encodeToString(byteArray,…
Ajith M A
  • 3,838
  • 3
  • 32
  • 55
30
votes
3 answers

What is the difference between PowerMock, EasyMock and Mockito frameworks?

I am very new to mocking framework, and my work needs mocking framework to finish unit testing. In the current code base i could see above 3 frameworks are being used in different places for unit testing. So, which one should i go for in the above 3…
keya
  • 2,068
  • 2
  • 18
  • 18
29
votes
2 answers

How to mock a static final variable using JUnit, EasyMock or PowerMock

I want to mock a static final variable as well as mock a i18n class using JUnit, EasyMock or PowerMock. How do I do that?
Gnik
  • 7,120
  • 20
  • 79
  • 129
28
votes
4 answers

Which Maven artifacts should I use to import PowerMock?

What jars do I need to add to my pom.xml to get PowerMock working with Mockito? I have the following dependencies: org.mockito mockito-all 1.9.0
user86834
  • 5,357
  • 10
  • 34
  • 47
27
votes
5 answers

How to mock an enum singleton class using Mockito/Powermock?

I am unsure on how to mock an enum singleton class. public enum SingletonObject{ INSTANCE; private int num; protected setNum(int num) { this.num = num; } public int getNum() { return num; } I'd like to stub getNum() in the…
Sonwright Gomez
  • 271
  • 1
  • 3
  • 3
26
votes
3 answers

Is it possible to use partial mocking for private static methods in PowerMock?

From the examples on the PowerMock homepage, I see the following example for partially mocking a private method with Mockito: @RunWith(PowerMockRunner.class) // We prepare PartialMockClass for test because it's final or we need to mock private or…
Rich Ashworth
  • 1,995
  • 4
  • 19
  • 29
26
votes
6 answers

mockito test gives no such method error when run as junit test but when jars are added manually in run confugurations, it runs well

I've been facing a peculiar problem. Basically, when I run my Mockito/PowerMockito test normally i.e. 'Run as Junit Test', it gives me the following error : java.lang.NoSuchMethodError: org.mockito.mock.MockCreationSettings.isUsingConstructor()Z at…
Siddharth Mehta
  • 445
  • 3
  • 7
  • 17
26
votes
1 answer

PowerMock: mock out private static final variable, a concrete example

what is the absolute minimal mocking that must be done to pass this test? code: class PrivateStaticFinal { private static final Integer variable = 0; public static Integer method() { return variable + 1;…
sam boosalis
  • 1,997
  • 4
  • 20
  • 32
25
votes
6 answers

Jacoco: For report generation the same class files must be used as at runtime

I have been working on an android project and use roboletric and powermock to do unitTests. When I run gradle jacocoTestReport, it will show [ant:jacocoReport] Classes in bundle 'app' do no match with execution data. For report generation the same…
alec.tu
  • 1,647
  • 2
  • 20
  • 41
25
votes
1 answer

Mocking static methods with PowerMock and Mockito

I'm trying to verify a call to java.sql.DriverManager.getConnection using JUnit, Mockito, and PowerMock. Here's my test case: @RunWith(PowerMockRunner.class) @PrepareForTest(DriverManager.class) public class MySQLDatabaseConnectionFactoryTest { …
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
24
votes
3 answers

PowerMock: stub methods from parent class

I'm using PowerMock and I'd like to know how to keep all behavior of the child class, but stub super calls that may be overriden by the child. Say I have this class: public class A { public String someMethod() { return "I don't want to…
jchitel
  • 2,999
  • 4
  • 36
  • 49