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

Testing LiveData using PowerMockRunner

My local unit tests use LiveData all the time. Normally, when you try to set a value on MutableLiveData you get java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. because local JVM has no access to Android framework.…
Michał Powłoka
  • 1,424
  • 1
  • 13
  • 31
3
votes
0 answers

Issue while using Vert.x and PowerMockito in Junit

I am writing JUnit tests for a Vert.x application. I need to use Powermock and Vert.x runner classes for my JUnit test. I am trying to use below code to integrate both…
Douglas A.
  • 31
  • 2
3
votes
3 answers

Use Mockito/PowerMockito to mock a public method of super class

Is there anyway in Mockito/PowerMockito to mock some methods of super class. Below is a scenario, it does a multilevel inheritance. public class Network { public String getNetwork() { return "Technology"; } } Here is another class…
Newbie
  • 1,403
  • 2
  • 13
  • 22
3
votes
0 answers

Using InjectMock on Object with private static final fields failing

I am writing test cases for a legacy code which for some reason we dont want to change. The code is something like this public class ToBeTested{ private static final String field1=SomeUtil.getProperty("somekey"); @AutoWired …
varkashy
  • 374
  • 4
  • 18
3
votes
2 answers

PowerMockito verify that a static method is never called

I am writing a JUnit test to verify that a static method (MyClass.myMethod()) is never invoked in the method flow. I tried doing something like this: PowerMockito.verifyStatic(Mockito.never()); MyClass.myMethod(Mockito.any()); In doing so I…
3
votes
2 answers

PowerMockito gives NoClassDefFoundError

I'm trying to set up a unit test with Mockito and PowerMockito, but it throws: Exception in thread "main" java.lang.NoClassDefFoundError: org/mockito/exceptions/Reporter whenever I try to run a test. These are my dependencies: testCompile…
aloj
  • 3,194
  • 7
  • 27
  • 38
3
votes
2 answers

warning using mockito-android

I got this warning using mockito You included the 'mockito-android' dependency in a non-Android environment. The Android mock maker was disabled. You should only include the latter in your 'androidTestCompile' configuration If disabling was a…
Jose M Lechon
  • 5,766
  • 6
  • 44
  • 60
3
votes
2 answers

Memory leak with powermockito simple test

I have the following powermock test: @RunWith(PowerMockRunner.class) @PrepareForTest({DaoCaseTypeDefinition.class, QDataContext.class}) public class PowermockTest { private static QDataContext m_dc; private static DaoCaseTypeDefinition…
mmelsen
  • 636
  • 1
  • 8
  • 24
3
votes
0 answers

powermockito "whenNew" call the answer immediately

When I use PowerMockito.whenNew...thenAnswer - the answer run immediately! my code: new Integer(9); PowerMockito.whenNew(Integer.class).withAnyArguments().thenAnswer(invocation -> { System.out.println("kuku int"); return…
Azriel Berger
  • 160
  • 4
  • 17
3
votes
0 answers

Java 8 filter() doesn't seem to work in class partially static mocked with PowerMockito

I'm using the Stream API to determine how many items in a list meet a certain condition, but when I attempt to unit test it, it fails. More specifically, I'm using the Stream API in a static method whose class I am statically mocking using…
bhawk90
  • 321
  • 1
  • 2
  • 7
3
votes
1 answer

Issue with Powermockito verifystatic

I have a legacy class which has a static void method which i need to test: public class A { public static void renameTo() { String ext = "." + this.fileName + ".backup"; for (File file : getCSVFile()) { …
Joe
  • 296
  • 1
  • 7
  • 19
3
votes
4 answers

Mockito and Deffered Result

I have a spring based project and I am trying to improve the code coverage within it I have the following block of code which uses a lambda on the defferedResult onCompletion method util.getResponse(userInfoDeferredResult, url, userName,…
Damien
  • 4,081
  • 12
  • 75
  • 126
3
votes
1 answer

UnfinishedStubbingException with PowerMockito

I want to test a method that is using a static method inside, that's why I am using PowerMockito. I want to mock a call to Request.Get from the Http Client Fluent API. I setup a simple test but I got an UnfinishedStubbingException and I don't…
Sydney
  • 11,964
  • 19
  • 90
  • 142
3
votes
1 answer

Powermock ExceptionInInitializerError with Junit

Unit Test setup @RunWith(PowerMockRunner.class) @PrepareForTest(PGWService.class) public class PGWServiceTest { public static final String TEST_CLIENTTRX_ID = "12345"; public static final int TEST_SITE_ID = 0; public static final long TEST_USER_ID =…
Ares Hisao
  • 33
  • 1
  • 3
3
votes
1 answer

Mockito spy failed because class member cannot be initialised

I have the following code: public class MyClass() { private MyObject myObject = getMyObject(); public MyObject getMyObject() { if (myObject == null) { myObject = MyStaticClass.createMyObject(); } return…
user1589188
  • 5,316
  • 17
  • 67
  • 130