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
19
votes
1 answer

PowerMock access private members

After reading: https://code.google.com/p/powermock/wiki/BypassEncapsulation i realized, i don't get it. See in this example: public class Bar{ private Foo foo; public void initFoo(){ foo = new Foo(); } } How can i access the…
Gobliins
  • 3,848
  • 16
  • 67
  • 122
19
votes
3 answers

How can I mock a void method to throw an exception?

I have a structure like this: public class CacheWrapper { private Map innerMap; public CacheWrapper() { //initialize the innerMap with an instance for an in-memory cache //that works on external server …
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
19
votes
3 answers

How to verify invocation of super.method() of parent class?

I have three very simple classes. One of them extends parent class. public class Parent{ protected String print() { // some code } } Here is a child class. public class Child extends Parent { /** * Shouldn't invoke…
Aaron
  • 1,294
  • 4
  • 14
  • 27
19
votes
2 answers

Powermock (With Easymock) no last call on a mock available

I am trying to just run a simple test case. I have the following method. public static void run(String[] args) throws Throwable { CommandLineArguments opts = CommandLineOptionProcessor.getOpts(args); } I will continue to build this method /…
dev
  • 1,477
  • 5
  • 18
  • 43
18
votes
4 answers

PowerMock: mocking of static methods (+ return original values in some particular methods)

I use PowerMock 1.4.7 and JUnit 4.8.2 I need to mock only some static methods and I want others (from the same class) just to return original value. When I mock with mockStatic and don't call when().doReturn() all static methods return their…
Michal Bernhard
  • 3,853
  • 4
  • 27
  • 38
18
votes
5 answers

Powermockito doNothing for method with arguments

I've developed an application in Java and I'm trying to create unit tests using Powermockito (I should add that I'm new to unit testing). I have a class called Resource which has a static method called readResources: public static void…
Anakin001
  • 1,226
  • 2
  • 14
  • 30
17
votes
2 answers

org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class for Andriod SSLContext.getInstance() metho

I am working on Android and PowerMock project. In this example, I am trying to mock the below method, but I am getting error org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class at…
PAA
  • 1
  • 46
  • 174
  • 282
17
votes
2 answers

PowerMockito verify private method called x times

I'm using PowerMockito and spy to mock private methods: final SomeClass someClass = new SomeClass(); final SomeClass spy = PowerMockito.spy(someClass); PowerMickito.doReturn("someValue", spy, "privateMethod1"); final String response =…
Kousha
  • 32,871
  • 51
  • 172
  • 296
17
votes
4 answers

How to android unit test and mock a static method

Hi I really hope you can help me, I feel like I've been pulling my hair out for days. I'm trying to write unit tests for a method A. Method A calls a static method B. I want to mock static method B. I know this has been asked before, but I feel…
James
  • 3,485
  • 3
  • 20
  • 43
17
votes
7 answers

how to mock a URL connection

Hi I have a method that takes an URL as an input and determines if it is reachable. Heres the code for that: public static boolean isUrlAccessible(final String urlToValidate) throws WAGNetworkException { URL url = null; …
Nemin
  • 1,907
  • 6
  • 24
  • 37
17
votes
1 answer

Using PowerMock and Robolectric - IncompatibleClassChangeError

I'm trying to use PowerMockito to mock some static methods in Android Robolectric tests. I'm using JUnit 4.8.2, Robolectric 2.2, Mockito 1.9.5, and PowerMock 1.9.5 as directed here. As I have to use the RoboElectricTestRunner, I'm attempting to…
C. Ross
  • 31,137
  • 42
  • 147
  • 238
17
votes
2 answers

PowerMock: How to unmock a method?

I have a static method that is mocked using PowerMock to throw an exception. (It deletes files.) Unfortunately, during my @After (after-each-test) method, I need to call this method without the mocks. How can I umock a method? I don't see an…
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
17
votes
1 answer

Can Powermockito mock final method in non-final concrete class?

Let's say I have a non-final concrete class with a final method like the one below. public class ABC { public final String myMethod(){ return "test test"; } } is it possible to mock myMethod() to return something else when it is called in…
sura watthana
  • 291
  • 3
  • 4
  • 6
16
votes
1 answer

mocking protected method

I want to mock an inherited protected method. I can't call this method directly from java code as it is inherited from class that in another package. I can't find a way to specify this method to stub in in when(...) package a; public class A() { …
michael nesterenko
  • 14,222
  • 25
  • 114
  • 182
16
votes
1 answer

Powermock keeps throwing errors for ScriptEngineManager

Due to the fact that I wanted to use Categories from JUnit on my tests I had to rewrite some mocked tests with PowerMock (powermock on top of EasyMock to mock statics). From RunWith(PowermockRunner.class) to the below; …
Jurn
  • 796
  • 1
  • 8
  • 19