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

NotAMockException when trying to verify a static method

I am trying to verify that a static method is not called in a certain configuration, in a unit test. I am thus using PowerMock (powermock-core:2.0.4 & powermock-module-junit4:2.0.4) and its Mockito API (powermock-api-mockito2:2.0.4). When doing…
EnzoMolion
  • 949
  • 8
  • 25
6
votes
2 answers

Unit Testing I can't add Observer to LiveData NullPointerException

Why i get nullPointerException despite if i remove observer and use mLivedata.getValue() and assertNotNull(mLiveData) it works well? @RunWith(PowerMockRunner.class) public class LoginRepositoryTest { //Data LoginRepository loginRepository; …
6
votes
0 answers

Junit Error : Invoking the beforeTestMethod method on PowerMock test listener

I am facing JUnit jar compatibility issue.initially, when I had created my JUnit classes below are the main jar versions. springframework.boot.version 1.3.0.RELEASE Mock - mockito-core-1.10.19.jar Power mockito - powermock-module-junit4-1.6.4.jar …
Amit
  • 497
  • 3
  • 8
  • 24
6
votes
0 answers

Upgrade Java 8 to 11 powermock issue

I am using surifire, failsafe, mockito and powermock. Where ever I tried I got like power mock is not support for Java 11. I have tried with the latest version of surefire also but it throws exception like. (build success if I skip the test cases)…
VG__
  • 533
  • 2
  • 6
  • 16
6
votes
1 answer

difference between Powermock and Powermockito

Can anyone could elaborate about PowerMock and PowerMockito. I didn't even get documentation for powermockito. Both used for mocking static and private methods in different way I guess. what are the similarities and usages? which…
anand krish
  • 4,281
  • 4
  • 44
  • 47
6
votes
1 answer

How to test a method returns boolean in Mockito

I am learning how to unit-testing in android studio. As shown below, I have a method called "isValidUrl" and in the testing section below, I coded the testing of that method using Mockito, but the test always fails. Can you please help and guild me…
Amrmsmb
  • 1
  • 27
  • 104
  • 226
6
votes
0 answers

Cannot mock instance of Firebase Analytics for Android

I'm trying to mock an instance of Firebase Analytics for Android unit testing, but my Firebase variable remains undefined after I call Firebase's getInstance() method - which should initialize an instance of the FirebaseAnalytics class. I'm using…
Brennan
  • 169
  • 8
6
votes
2 answers

PowerMockito .when().thenReturn() with randomUUID not returning expected value

I'm trying to test a Web Service method which connects to a SQL Server Database which contains JCR nodes, as we're using JackRabbit. The method looks like: public String addDocumentByJson(String fileName, byte[] fileContent, int status, String…
Frakcool
  • 10,915
  • 9
  • 50
  • 89
6
votes
3 answers

How do I mock an implementation class?

I have something like this: public interface SomeInterface { public String someMethod(String someArg1, String someArg2); } public class SomeInterfaceImpl { @Override public String someMethod(String someArg1, String someArg2) { …
user87407
  • 647
  • 3
  • 7
  • 24
6
votes
1 answer

Mock Spring Component

I am using Mockito to mock spring beans. It works fine when I mock an interface. In our application, there are few @Component beans which do not implement any interface. When I try to mock such component , the spring context tries to inject the…
lives
  • 1,243
  • 5
  • 25
  • 61
6
votes
1 answer

Handle anonymous class with Generics in Mockito

I am trying to write unit test using Powermockito for following method - public String getGenerator(String json) throws IOException { String jwt = ""; ObjectMapper mapper = new ObjectMapper(); // convert JSON string to Map …
Saurabh
  • 2,384
  • 6
  • 37
  • 52
6
votes
1 answer

PowerMockito.when returns null

I am not sure why PowerMockito.when returns null. This is my class under test: public class A { public Integer callMethod(){ return someMethod(); } private Integer someMethod(){ //Some Code HttpPost httpPost = new…
Sumit
  • 856
  • 5
  • 18
  • 38
6
votes
3 answers

PowerMockRunner does not apply JUnit ClassRules

I need to mock a static method of a class and use that mocked method in my test. Right now seems I can only use PowerMock to do that. I annotate the class with @RunWith(PowerMockRunner.class), and @PrepareForTest with the appropriate class. In my…
Yusuf Azal
  • 218
  • 1
  • 14
6
votes
2 answers

Mocking CountDownTimer and Handler

I'm using Mockito and PowerMockito to write my Unit Test on Android. I've managed to work with almost all by back stuff, but I'm stuck with the CountDownTimer and Handler. I'm able to mock a subclass of CountDownTimer, but I'm not able to mock…
Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119
6
votes
4 answers

Real method getting called when Mockito doNothing method is called

I am trying to mock the class KeyStore. After mocking I do not want anything to happen when the load method if it has been called. Therefore I wrote the below lines to achieve this. @PrepareForTest(KeyStoreFactory.class) @Test …
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118