Questions tagged [powermockito]

Use this tag for questions about PowerMockito, a Java Framework that allows the mocking of usually un-mockable types, i.e. statics, and private methods, in conjunction with the Mockito framework. Questions about using PowerMock with EasyMock should be tagged [powermock] instead.

PowerMockito is used in addition with the Java Framework Mockito. PowerMockito's main strength is the ability to mock Static and Private Methods. A good example of PowerMocktio is the ability to mock Singletons which are generally difficult when unit testing.

Heres a great link to starting out with PowerMockito: Using PowerMock with Mockito

1326 questions
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
14
votes
3 answers

mock-maker-inline - Could not initialize inline Byte Buddy mock maker (power-mockito + jdk 8 + bytebuddy)

I am using Powermockito to mock and spy static classes. Version: 2.0.4 Also using mockito-core version 3.0.0. In, one of my projects, i am able to mockStatic by enabling mock-maker-inline as specified…
DecKno
  • 293
  • 1
  • 5
  • 21
14
votes
2 answers

ClassCastException exception when running Robolectric test with Power Mock on multiple files

So I set up the power mock based on the reference guide here. It all seems to run perfectly fine with a single test class. But when executing multiple JUnit tests I am getting the following error on the second test class. As you can see from the…
13
votes
2 answers

PowerMockito: NotAMockException on a mock

Bit of a complicated setup. Robolectric, PowerMockito rule-based config. @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) // Using…
Jon O
  • 6,532
  • 1
  • 46
  • 57
13
votes
4 answers

PowerMockito.verifyStatic() Problems

I need to use PowerMockito to test if a specific static method is called. I am using the following PowerMockito and JUnit libraries ... powermock-mockito-1.6.2-full.jar junit-4.12.jar I am having issues getting the PowerMockito.verifyStatic()…
John A Qualls
  • 739
  • 1
  • 6
  • 17
12
votes
1 answer

PowerMockito VerifyStatic not working in 2.0.0-beta5

I am getting an issue using powermockito (2.0.0-beta5) to verify a static method was called a certain number of times when I call a different (also static) method. The classes are prepared for test at the top of my test file The relevant code…
jbird
  • 506
  • 6
  • 21
12
votes
1 answer

PowerMockito+Junit - Mocking System.getenv

The line i try to mock in class is: String x[] = System.getenv("values").split(",") for(int i=0;i<=x.length;i++){ //do something } As far I have written is as follows: @RunWith(PowerMockRunner.class) @PrepareForTest({System.class}) public…
Gayathri
  • 1,776
  • 5
  • 23
  • 50
12
votes
1 answer

PowerMockito throwing ClassNotPreparedException even with @PrepareForTest using Scala Test

I have the following test... import org.scalatest.junit.JUnitRunner ... @PowerMockRunnerDelegate(classOf[JUnitRunner]) @PrepareForTest(Array(classOf[AuditLog])) class ConnectorAPITest extends path.FreeSpec with ShouldMatchers { "Mocked Tests" - { …
Jackie
  • 21,969
  • 32
  • 147
  • 289
12
votes
3 answers

Cannot use PowerMockRunner with SpringBootTest

For my project, i am trying to write integration test using SpringBootTest. I have 3rd party static final class. I tried mock it with Power mock. But when try to using it @SpringBootTest, exception is thrown. Here is my sample…
Ramp
  • 140
  • 1
  • 1
  • 8
11
votes
8 answers

Mockito cannot mock this class: interface

I'm having an issue with mockito and powermock, I can mock an abstract class with a final static method with no problems. When trying to mock an Interface as with WebIServerSession I'm getting the stacktrace below. I've had a look at other issues in…
Marcos
  • 701
  • 1
  • 8
  • 25
11
votes
2 answers

java.lang.NoClassDefFoundError: Could not initialize class org.mockito.internal.util.MockUtil

I want to use testng + powermock + spring to run test code,my jar version is: powermock = 1.6.2 powermock-module-testng = 1.6.2 mockito = 1.10.19 testng = 6.8.7 spring = 4.1.1.RELEASE JDK = 1.8.0 And my test code as follows,and not extends…
chen
  • 119
  • 1
  • 1
  • 5
11
votes
1 answer

Verify Static Method Call using PowerMockito 1.6

I am writing JUnit test case for methods similar to sample given below: Class SampleA{ public static void methodA(){ boolean isSuccessful = methodB(); if(isSuccessful){ SampleB.methodC(); } } public…
Prerak Tiwari
  • 3,436
  • 4
  • 34
  • 64
11
votes
1 answer

How do I return different values on different calls to a mock?

I have the following code which is getting the current counter value from DB. Then it updates the counter in DB and then again it retrieves the value. int current = DBUtil.getCurrentCount(); DBUtil.updateCount(50);// it updates the current count by…
user2032118
  • 455
  • 3
  • 6
  • 16
10
votes
1 answer

Thread.sleep is not getting mocked with PowerMockito

Here's my setup which is not working: SomeclassTest.java @RunWith(PowerMockRunner.class) @PowerMockIgnore("javax.management.*") @PrepareForTest({SomeclassTest.class, Someclass.class}) public class SomeclassTest{ @InjectMocks private Someclass…
Muku
  • 538
  • 4
  • 18
10
votes
2 answers

Realm Unit Testing

I am trying to unit test Realm and its interactions but things are not going too well. I have included all dependencies and keep getting vague failures, below is my code for the Helper class which is a wrapper over Realm. Questions Is this the…
Zee
  • 103
  • 1
  • 1
  • 8
1
2
3
88 89