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
168
votes
9 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
  • 2,360
  • 2
  • 17
  • 23
59
votes
6 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
  • 917
  • 1
  • 8
  • 13
36
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,412
  • 2
  • 27
  • 52
35
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,…
Arar
  • 1,926
  • 5
  • 29
  • 47
32
votes
7 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
  • 4,297
  • 6
  • 41
  • 67
22
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
  • 3,561
  • 9
  • 29
  • 45
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
21
votes
3 answers

Mock Static Methods in JUnit5 using PowerMockito

Need help for Mocking Static methods using JUnit5 with PowerMockito framework. Powermock junit5 and mockito2.x not working RunnerTestSuiteChunker not found import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import…
chaitanya
  • 1,726
  • 2
  • 17
  • 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
18
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
  • 31,819
  • 51
  • 183
  • 302
17
votes
2 answers

How to mock UUID.randomUUID() from java.util package?

What is wrong with the UUID.randomUUID() - it just can't be mocked Is it possible to mock? Or i have an error in my source? Look at example: 1) Class that is tested package com.grayen; import java.util.UUID; public class TestedClass { public…
Maksim Petrov
  • 313
  • 1
  • 2
  • 7
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
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
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
1
2 3
88 89