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

Equivalent of Mockito any with not null constraint

I am writing test cases from last few days. I want to know is there any equivalent of org.mockito.Matchers.any with not null constraint? I mean if you say: when(reviewBuilder.saveReviewModel(any())).thenReturn(2L); Then saveReviewModel can take…
user3681970
  • 1,201
  • 4
  • 19
  • 37
16
votes
2 answers

Powermock after log4j2.3 upgrade Could not reconfigure JMX java.lang.LinkageError

Im upgrading log4j from 1.x to 2.3. after setting up maven dependencies my project is builing and application is running fine. But while builing, at test goal getting below mentioned error in variouse unit Test classes which are already there. I…
16
votes
3 answers

Stub value of Build.VERSION.SDK_INT in Local Unit Test

I am wondering if there is anyway to stub the value of Build.Version.SDK_INT? Suppose I have the following lines in the ClassUnderTest: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { //do work }else{ //do another work } How…
Evgeniy Mishustin
  • 3,343
  • 3
  • 42
  • 81
16
votes
1 answer

Bad type on the operand stack in arraylength

I am trying to test a method using powermock. i haven't written any test cases yet. Just trying to set up the class for Mocking. here is what I have so far: @RunWith(PowerMockRunner.class) @PrepareForTest({ ReadRubric.class }) public class…
Nemin
  • 1,907
  • 6
  • 24
  • 37
16
votes
4 answers

PowerMockito and Java 8 ZonedDateTime toInstant() not found

Wen I run my tests: @RunWith(PowerMockRunner.class) @PrepareForTest(MyStuff.class) public class MyStuffTest { ..whatever After I added ZonedDateTime class to that code it stared failing with the following error: java.lang.IllegalStateException:…
ses
  • 13,174
  • 31
  • 123
  • 226
16
votes
2 answers

How to use PowerMock in Android projects?

I created a new Android test project. I downloaded powermock-mockito-junit-1-1.5.zip from https://code.google.com/p/powermock/downloads/list. I added all of the libraries to the test project's libs folder. The test class is a very simple…
Matt Quigley
  • 7,614
  • 4
  • 25
  • 26
15
votes
2 answers

Powermock Compatibility with JDK 17

Recently I was upgrading my project from JDK 11 to JDK 17. After upgrading, powermock seems to have an issue. While running AUT's , I am getting following error: java.lang.RuntimeException: PowerMock internal error: Should never throw exception at…
sanchit relan
  • 151
  • 1
  • 1
  • 5
15
votes
1 answer

Mocking static classes with Powermock2 and Kotlin

in Android I've been using the version 1.6.1 of Powermock and all this implementation worked really good for statics. It's not working now at all when I changed to 2.0.0-beta.5. Indeed, it didn't even work upgrading from my previous 1.6.1 to…
Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92
15
votes
1 answer

PowerMockito (with Mockito) failing with ExceptionInInitializerError

We are using Powermockito with Mockito to mock some static classes. There seems to be java.lang.ExceptionInInitializerError thrown every time. Can you help me identify where the problem is? Java class under test package…
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
15
votes
1 answer

Failing integration test for Apache Spark Streaming

I've been trying to track down an issue with some unit/integration tests I've been writing for an Apache Spark project. When using Spark 1.1.1 my test passed. When I tried to upgrade to 1.4.0 (also tried 1.4.1) the test starts failing. I've managed…
Glen
  • 21,816
  • 3
  • 61
  • 76
15
votes
1 answer

Mock private method using PowerMockito

I'm using PowerMockito to mock the private method call (privateApi) but it still makes the privateApi call which in turn makes another thirdPartCall. I'm getting into problem when thirdPartyCall throws exception. As far as I understand, if I'm…
Pankaj
  • 3,512
  • 16
  • 49
  • 83
15
votes
1 answer

Mocking Joda DateTime method using Mockito

I want millis to return specified value. public long myMethod(){ DateTime nowDateTime = new DateTime(DateTimeZone.UTC); long millis = nowDateTime.getMillis(); System.out.println(millis); } I tried this with no…
shankshera
  • 947
  • 3
  • 20
  • 45
15
votes
7 answers

Mock System class to get system properties

I have a folder path set in system variable through JVM arguments in Eclipse and I am trying to access it in my class as: System.getProperty("my_files_path"). While writing junit test method for this class, I tried mocking this call as test…
15
votes
1 answer

Mockito throws NullPointer when creating a mock object

I have an integration test in which some set up is done using Guice. I'm using Mockito to mock some of the dependencies. This has worked fine for me until now. I needed to use PowerMock for some other dependency. Now Mockito is throwing a…
Jorn
  • 20,612
  • 18
  • 79
  • 126
15
votes
5 answers

Troubles with SureFire plugin: -"The forked VM terminated without saying properly goodbye. VM crash or System.exit called ? "

While running of unit tests following exception occurs: org.apache.maven.lifecycle.LifecycleExecutionException: ExecutionException; nested exception is java.util.concurrent.ExecutionException: java.lang.RuntimeException: The forked VM terminated…
johnny-b-goode
  • 3,792
  • 12
  • 45
  • 68