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
199
votes
7 answers

Mockito + PowerMock LinkageError while mocking system class

I've got such a code snippet: @RunWith(PowerMockRunner.class) @PrepareForTest({Thread.class}) public class AllMeasuresDataTest { @Before public void setUp() throws Exception { } @Test public void testGetMeasures() { AllMeasuresData measure =…
Wojciech Reszelewski
  • 2,656
  • 2
  • 18
  • 27
127
votes
3 answers

Using PowerMockito.whenNew() is not getting mocked and original method is called

I have a code somewhat like this below: Class A { public boolean myMethod(someargs) { MyQueryClass query = new MyQueryClass(); Long id = query.getNextId(); // some more code } } Class MyQueryClass { .... public Long…
user3942446
  • 1,273
  • 2
  • 9
  • 4
119
votes
1 answer

PowerMockito mock single static method and return object

I want to mock a static method m1 from a class which contains 2 static methods, m1 and m2. And I want the method m1 to return an object. I tried the following 1) PowerMockito.mockStatic(Static.class, new Answer() { @Override …
user1393653
  • 1,191
  • 2
  • 8
  • 3
111
votes
7 answers

Mock a constructor with parameter

I have a class as below: public class A { public A(String test) { bla bla bla } public String check() { bla bla bla } } The logic in the constructor A(String test) and check() are the things I am trying to mock. I…
Shengjie
  • 12,336
  • 29
  • 98
  • 139
111
votes
4 answers

Mock static methods from multiple class using PowerMock

I know how to mock static methods from a class using PowerMock. But I want to mock static methods from multiple classes in a test class using JUnit and PowerMock. Can anyone tell me is it possible to do this and how to do it?
Newbie
  • 2,979
  • 9
  • 34
  • 41
91
votes
5 answers

How do I mock a static method that returns void with PowerMock?

I have a few static util methods in my project, some of them just pass or throw an exception. There are a lot of examples out there on how to mock a static method that has a return type other than void. But how can I mock a static method that…
Pete
  • 10,720
  • 25
  • 94
  • 139
83
votes
1 answer

Can I use Cobertura on Unit Tests with PowerMock?

Problem I am setting-up unit-test code coverage for an Android library which uses Robolectric to run the tests and PowerMock/Mockito for mock-testing. However, running unit-tests with Cobertura results in the following…
PLNech
  • 3,087
  • 1
  • 23
  • 52
76
votes
9 answers

Mocking Logger and LoggerFactory with PowerMock and Mockito

I have the following Logger I want to mock out, but to validate log entries are getting called, not for the content. private static Logger logger = LoggerFactory.getLogger(GoodbyeController.class); I want to Mock ANY class that is used for…
Mick Knutson
  • 2,297
  • 3
  • 25
  • 48
74
votes
6 answers

How to mock private method for testing using PowerMock?

I have a class which I would like to test with a public method that calls private one. I'd like to assume that private method works correctly. For example, I'd like something like doReturn....when.... I found that there is possible solution using…
Julie Langstrump
  • 877
  • 1
  • 9
  • 10
63
votes
5 answers

PowerMock + Mockito VS Mockito alone

Can anyone please summarize, what exactly features gives you adding PowerMock on top of the Mockito? So far I've found these: mock static, final and private methods remove static initializers allow mocking without dependency injection - this one…
Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
59
votes
6 answers

How to mock a Kotlin singleton object?

Given a Kotlin singleton object and a fun that call it's method object SomeObject { fun someFun() {} } fun callerFun() { SomeObject.someFun() } Is there a way to mock call to SomeObject.someFun()?
user3284037
  • 917
  • 1
  • 8
  • 13
57
votes
6 answers

PowerMock testing - set static field of class

I'm having difficulty finding a way to set a static field of a class. It's basically like this: public class Foo{ // ... private static B b = null; } where B is another class. Is there any way to do this in PowerMock other than with…
Craig Anderson
  • 611
  • 2
  • 7
  • 6
55
votes
4 answers

Unable to run JUnit test with PowerMockRunner

I have a Gradle based Java project were I now want to mock a private method using PowerMock. The problem is that I am not able to use the PowerMockRunner as I always get the following exception when I add the…
Ben
  • 934
  • 2
  • 8
  • 11
52
votes
4 answers

How to mock static method without powermock

Is there any way we can mock the static util method while testing in JUnit? I know Powermock can mock static calls, but I don't want to use Powermock. Are there any alternatives?
gati sahu
  • 2,576
  • 2
  • 10
  • 16
51
votes
7 answers

AndroidStudio/Gradle with powermock

I couldn't find any info on how to setup powermock with Android Studio/Gradle. Everything I've tried resulted in build exceptions. Could anybody show a correct way to do it? Thanks.
midnight
  • 3,420
  • 3
  • 36
  • 58
1
2 3
99 100