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
23
votes
3 answers

Powermock mockstatic Cannot subclass final class

I am trying to mock a final class PowerMockito.mockStatic(TestFinalClass.class); It is working from my eclipse when I run a single junit and add javaagent to my VM arguments -javaagent:{path}/powermock-module-javaagent-1.6.4.jar But when I try to…
user3755282
  • 813
  • 2
  • 9
  • 15
23
votes
2 answers

Mocking a Private Variable that is Assumed to Exist

How can you get a mock object in at runtime when it is not created/initialized in the class you are testing, it is not static (singleton pattern), or you don't have some sort of test constructor to hook into? In a class that I am writing some unit…
Walls
  • 3,972
  • 6
  • 37
  • 52
23
votes
4 answers

powermock mocking constructor via whennew() does not work with anonymous class

I have a DummyResource class and a DummyTarget file, and a test class TestDummyResource as below, but the mocked object DummyResource dr = mock(DummyResource.class) only works when I call the constructor inside a normal class, when it's called in an…
Shengjie
  • 12,336
  • 29
  • 98
  • 139
22
votes
5 answers

Mock static method in JUnit 5 using Mockito

I am trying to mock static classes(org.apache.commons.beanutils.Beanutils) for Junit 5 test cases. I came across mockito-inline dependency helps with mocking static classes. I tried to use the mockito-inline in the project for some strange reason it…
22
votes
3 answers

Extension API internal error: org.powermock.api.extension.reporter.MockingFrameworkReporterFactoryImpl

I'm trying to write a unit test using PowerMockRunner but I got the following error: java.lang.IllegalStateException: Extension API internal error: org.powermock.api.extension.reporter.MockingFrameworkReporterFactoryImpl could not be located in…
Thiago Gonzaga
  • 469
  • 1
  • 3
  • 7
22
votes
4 answers

The type MockitoAnnotations.Mock is deprecated

I am mocking a object using @Mock annotation @Mock Customer customer; but the @Mock annotation is showing warning The type MockitoAnnotations.Mock is deprecated and my test case is failed
Girdhar Singh Rathore
  • 5,030
  • 7
  • 49
  • 67
22
votes
3 answers

Bad method call from inside of a branch

After upgrading JDK to java7u65 several unit-tests utilizing Mockito and PowerMock started to fail with following causes: 15:15:23,807 INFO - Tests in error: 15:15:23,810 INFO - initializationError(com.your.ClassHere): Bad method call…
Lauri
  • 1,859
  • 2
  • 14
  • 17
22
votes
4 answers

How to verify that an exception was not thrown

In my unit test using Mockito I want to verify that NullPointerException was not thrown. public void testNPENotThrown{ Calling calling= Mock(Calling.class); testClass.setInner(calling); testClass.setThrow(true); …
well_i
  • 369
  • 2
  • 4
  • 11
21
votes
4 answers

Testing private method using power mock which return list of Integers

I have a private method which take a list of integer value returns me a list of integer value. How can i use power mock to test it. I am new to powermock.Can i do the test with easy mock..? how..
user882196
  • 1,691
  • 9
  • 24
  • 39
21
votes
4 answers

PowerMock and Java 8 issue: InterfaceMethodrefInfo cannot be cast to MethodrefInfo

I´m having issues while trying to execute a unit test using PowerMock with Mockito. I need PowerMockito to mock an static method. These are the versions I´m using: PowerMock 1.6.2 Mockito 1.10.19 JUnit 4.12 Java 8 When I add the annotation…
KurroGB
  • 431
  • 2
  • 4
  • 8
21
votes
1 answer

Why not PowerMock

I am working on a project where we are currently doing testing with JUnit and Mockito. Now I am trying to find out what would be the side effects of adding PowerMock to the mix. What are its disadvantages, any dependencies I should know about, any…
KKO
  • 1,913
  • 3
  • 27
  • 35
21
votes
7 answers

PowerMock throws NoSuchMethodError (setMockName)

I'm trying to mock a constructor using PowerMockito but every time I run the test I get the following error: java.lang.NoSuchMethodError:…
Psycho Punch
  • 6,418
  • 9
  • 53
  • 86
20
votes
1 answer

Mockito / PowerMocktio doNothing for none void method

I need a method which returns something to do nothing when invoked during testing, the class instance which owns the method is implemented as a spy. I am aware the doNothing() method only works with void methods. Is there a way to get the same …
Mat
  • 515
  • 3
  • 8
  • 13
20
votes
5 answers

Mock static method with GroovyMock or similar in Spock

First-timer here, apologies if I've missed anything. I'm hoping to get around a call to a static method using Spock. Feedback would be great With groovy mocks, I thought I'd be able to get past the static call but haven't found it. For background,…
alexgibbs
  • 2,430
  • 2
  • 16
  • 18
19
votes
1 answer

Generating HmacSHA256 signature in JUnit

I'm trying to sign my message to Amazon AWS (inside JUnit test), but I encountered a problem. Here's the code I'm using: String secretAccessKey = "secret1234678901"; SecretKeySpec keySpec = new SecretKeySpec(secretAccessKey.getBytes(UTF-8),…
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115