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
10
votes
2 answers

How to stub return value for the private method of same class using mockito

I am working on spring based project and writing unit test case using JUnit + Mockito. I am facing a problem while stubbing boolean value to a private method of same test class (after changing access level to public, still I have failed to stub the…
Rajesh Hatwar
  • 1,843
  • 6
  • 39
  • 58
10
votes
1 answer

mockito: The class [X] not prepared for test

After migrating my JAVA EE app. (Spring Web model-view-controller (MVC) framework) from Ant to Maven I have some errors executing the Junit test that before the migration were working fine org.powermock.api.mockito.ClassNotPreparedException: The…
Amadeu Cabanilles
  • 913
  • 3
  • 19
  • 47
10
votes
1 answer

Robolectric + PowerMock + Mockito not working

I'm struggling to get this rare combination works, but I haven't got luck. My project uses JUnit + Mockito + Robolectric for testing and Roboguice for injection, when I added PowerMock to the equation weird things happened, this are some of the…
JavierSP1209
  • 899
  • 8
  • 17
10
votes
2 answers

mockito how to verify using methodname and reflection

I have a spy or a mock of an object and I wants to verify that a method has been call problem, I receive methodname at execution Time not compilation time I would like to do something like: SimpleObj mockObject= Mockito.mock(SimpleObj.class); …
Guillaume B.
  • 133
  • 1
  • 7
10
votes
4 answers

PowerMockito mocking static method fails when calling method on parameter

I'm trying to test a class which uses a calculator class with a number of static methods. I've successfully mocked another class in a similar way, but this one is proving more stubborn. It seems that if the mocked method contains a method call on…
maccaroo
  • 819
  • 2
  • 12
  • 22
10
votes
1 answer

How to get the number of times a mock is called in Mockito

I'm using PowerMockito with Mockito to mock a few static classes. I want to get the number of times a particular mock object is called during run time so that I can use that count in verify times for another mock object. I need this because, the…
TechCrunch
  • 2,924
  • 5
  • 45
  • 79
9
votes
5 answers

JUINT test giving error java.lang.reflect.InaccessibleObjectException: Unable to make protected void java.lang.Object.finalize()

While running the test I am getting the error , I am not able to understand why am I getting this error , this code is working with fine in java 8 , while running it in java 17 it is giving error. googled this error but found nothing useful. Please…
The_new_coder
  • 263
  • 2
  • 3
  • 13
9
votes
1 answer

How do I mock Authentication objects in PowerMockito?

Note that this is a UNIT test, not an integration test so I don't want any Spring and literally everything that can be mocked, should be. This code: @RunWith(PowerMockRunner.class) @PrepareForTest({...}) @PowerMockIgnore({ …
user6032518
9
votes
1 answer

NoSuchMethodError in powermock spring boot?

Trying to Mock private method "mapCustomerToRule" in the same class with PowerMockito Spy but not able to get it. While it gives NoSuchMethodError. but it still makes the private method call which in turn makes another thirdPartCall. I'm getting…
Anirudh Jadhav
  • 967
  • 1
  • 9
  • 23
9
votes
4 answers

How to test a static method using mock() and spy()

the below posted method in the code section contains a static method which is "with()". I want to test the code in below, so I coded the test of this method as shown in the testing section. i tried to test the method using both of "spy()" and…
Amrmsmb
  • 1
  • 27
  • 104
  • 226
9
votes
0 answers

PowerMock in scala

I want to mock a scala companion object, so im trying to use PowerMockito. I have the following: import org.junit.runner.RunWith import org.mockito.Mockito.when import org.powermock.core.classloader.annotations.PrepareForTest import…
9uzman7
  • 409
  • 8
  • 19
9
votes
2 answers

How to mock keystore class and assign mock behavior to its methods?

I have the below method which I need to write unit tests for. But I cannot spy the class KeyStore. It throws the below exception. org.mockito.exceptions.base.MockitoException: Unable to create mock instance of type 'KeyStore' I can mock it though.…
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
9
votes
3 answers

How can I mock an instance of an enum class with PowerMock & Mockito?

I tried to follow the example offered in the answer to this very similar question, but it does not work for me. I get the following error message: java.lang.IllegalArgumentException: Cannot subclass final class class…
Selena
  • 2,208
  • 8
  • 29
  • 49
9
votes
1 answer

Hybris Mockito : Getting Exception there is no LocaleProvider for (detached) model

I am writing test cases at facade level in my Hybris Project. I am creating model instance and setting name and code. Model is having some attributes Localized, because of that I am getting no LocaleProvider…
Free-Minded
  • 5,322
  • 6
  • 50
  • 93
8
votes
0 answers

PowerMockito 2 on Java 11 error when running test: class javax.xml.parsers.FactoryFinder cannot access class jdk.xml.internal.SecuritySupport

I get an error when running my PowerMockito2 tests on Java 11. I was able to resolve this error by putting this annotation at class level but I do not want to do this. Is there a way not to get this error without using…
javaPlease42
  • 4,699
  • 7
  • 36
  • 65
1 2
3
88 89