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

PowerMockito how to capture a parameter passed to a mock object?

I'm trying to capture a parameter passed in input to a mock object with PowerMockito, this is the code: //I create a mock object ClassMocked mock = PowerMockito.mock(ClassMocked.class); //Create the captor that will capture the String passed in…
user2572526
  • 1,219
  • 2
  • 17
  • 35
3
votes
0 answers

How to test AsyncTask class using Mockito

I have AsyncTask class as shown below in the code, and I am trying to test it. I coded the test cases of the AsyncTask as shown below in the testing section, but as shown in the testing code, I just tested whether or not the AsyncTask methods was…
Amrmsmb
  • 1
  • 27
  • 104
  • 226
3
votes
1 answer

Accessing nested private static class in Java

I have following public class in Java which looks like: public class MyClass { private static class MyNestedClass extends SomeOtherClass { } } I am writing a test where I need to create a mock object for MyNestedClass class using…
tech_human
  • 6,592
  • 16
  • 65
  • 107
3
votes
1 answer

PowerMockito with Jacoco Code Coverage

Code Coverage with Powermockito with jacoco In my project we using powermockito, for codecoverage jacoco api. Seens we we use @preparefortest({xyzimpl.class,abcd.class}) annotation. On code coverage, xyzimpl.java code coverage is not covered. is…
KrishnaFJ
  • 155
  • 1
  • 2
  • 8
3
votes
3 answers

How to test void method with private method calls using Mockito

I have the following piece of code public class A extends B { private boolean workDone = false; @Override public void publicMethod(boolean flag) { if (!workDone) { privateMethod(); workDone = true; } super.publicMethod(flag); } …
Ankit
  • 115
  • 1
  • 3
  • 12
3
votes
1 answer

PowerMockito.whenNew is not working

Hi folks i am new to PowerMockito and i am trying to use whenNew in PoweMockito and its not working for me, can anyone please help me out with this?? Below is my Test method which is used to test Class2, and i have Used PowerMockito.whenNew for…
kaushik
  • 33
  • 1
  • 1
  • 4
3
votes
2 answers

Powermock: NoClassDefFoundError when trying to mock static classes

I'm trying to understand how to use Powermock. I'm trying to realize the example of Static method mocking here. I created this code based on the example above. I however get a NoClassDefFoundError when trying to run the test. I don't know what…
Philippe
  • 1,715
  • 4
  • 25
  • 49
3
votes
1 answer

Powermock: Notifications are not supported when all test-instances are created first

When I try to execute the below simplified JUnit test, it succeeds but i am getting an error message: Notifications are not supported when all test-instances are created…
M. Abbas
  • 6,409
  • 4
  • 33
  • 46
3
votes
3 answers

isEmpty() checking in mockito

How to write junit for the statement if (!messageMap.isEmpty()) where messageMap is a TreeMap. I wrote as follows, PowerMockito.when(!messageMap.isEmpty()).thenReturn(true); But this code not working.
Nidheesh
  • 47
  • 1
  • 2
  • 10
3
votes
1 answer

How to Mock mock Spring JDBC when we pass with parameter (RowMapper)

This is my class @Repository public class JdbcRolesDao implements RolesDao{ private JdbcTemplate jdbcTemplate; private static final String GET_USER_ROLES_QUERY = "select ROLE_CD from USER_ROLES where USER_ID = ? AND USER_DRCTRY =…
radha r
  • 115
  • 1
  • 3
  • 9
3
votes
1 answer

Cannot run Robolectric test in Android Studio using Realm + PowerMockito

I was using Realm, PowerMockito and Robolectric to write test for my app. The test is running fine if I use ./gradlew test, but if I run with the configuration in Android Studio. It will show error as…
i3irth
  • 35
  • 3
3
votes
1 answer

Power Mockito returns error org.powermock.api.mockito.ClassNotPreparedException

I have an image loader class and i wanted to test some static methods in it. Since Mockito does not support static methods i switched to Power Mockito. However the static method i am testing has a method call Base64.encodeToString(byteArray,…
Ajith M A
  • 3,838
  • 3
  • 32
  • 55
3
votes
1 answer

Powermockito with Java 8

I am using powermockito to mock static methods with params in Java 7. Recently started migrating to Java 8. Post migration the powermockito stopped mocking the static methods and started calling the original method. Pom.xml
3
votes
1 answer

How to have more than 1 Class in PowerMock SuppressStaticInitializationFor Annotation

How to have more than 1 Class in PowerMock SuppressStaticInitializationFor class level Annotation. This works fine with 1 class. @SuppressStaticInitializationFor("org.foo.FooClass") I can't figure out how to put more than 1 class in this annotation…
javaPlease42
  • 4,699
  • 7
  • 36
  • 65
3
votes
2 answers

Using Mockito: Matching multiple arguments in a private static method?

I've been trying to use Mockito and PowerMockito to test my code. I have something akin to the following class: public class asdfClass{ public static String methodToMock(String item, String otheritem){ return "asdf"; } public…
user3537932
  • 153
  • 1
  • 11