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

1204 questions
121
votes
6 answers

Mockito: Mock private field initialization

How I can mock a field variable which is being initialized inline? class Test { private Person person = new Person(); ... public void testMethod() { person.someMethod(); ... } } Here I want to mock…
Arun
  • 1,822
  • 2
  • 14
  • 23
39
votes
5 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
  • 717
  • 1
  • 7
  • 13
26
votes
1 answer

Mocking a method which returns Page interface

I have a method which I need to write unit test case. The method returns a Page type. How can I mock this method? Method: public Page findAllCompany( final Pageable pageable ) { return…
Naanavanalla
  • 1,119
  • 2
  • 16
  • 40
21
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
  • 449
  • 1
  • 3
  • 7
20
votes
4 answers

PowerMock & Java 11

We are using PowerMock in few of our historical projects. Unfortunately PowerMock is quite dead and is not compatible with Java 11. And we are using mockStatic(). Yes, we know its considered harmful - its in the legacy code and we would prefer not…
malejpavouk
  • 3,858
  • 3
  • 36
  • 64
20
votes
3 answers

@RunWith(PowerMockRunner.class) vs @RunWith(MockitoJUnitRunner.class)

In the usual mocking with @Mock and @InjectMocks annotations, the class under testing should be run with @RunWith(MockitoJUnitRunner.class). @RunWith(MockitoJUnitRunner.class) public class ReportServiceImplTestMockito { @Mock private…
Johan
  • 2,763
  • 7
  • 23
  • 33
17
votes
3 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,350
  • 2
  • 12
  • 16
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
  • 13,240
  • 23
  • 104
  • 175
16
votes
5 answers

MockClassLoader cannot access jdk/internal/reflect superclass jdk.internal.reflect.MagicAccessorImpl

I am in the middle of migrating a project into Java9, The Tests start failing after I switched to the new Java version, it seems like PowerMock is trying to access some classes it does not have access to. Tests run: 1, Failures: 0, Errors: 1,…
Saif Masadeh
  • 1,235
  • 2
  • 15
  • 32
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
  • 9,668
  • 27
  • 81
  • 125
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
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
  • 4,769
  • 6
  • 41
  • 79
13
votes
1 answer

How to mock constructor with PowerMockito

I'm trying to mock a class constructor with PowerMockito for first time, but it doesn't work. My current code is: public class Bar { public String getText() { return "Fail"; } } public class Foo { public String getValue(){ …
Addev
  • 28,535
  • 43
  • 166
  • 288
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,267
  • 1
  • 41
  • 54
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
1
2 3
80 81