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
5
votes
0 answers

why i am getting org.powermock.api.mockito.expectation.WithOrWithoutExpectedArguments?

Currently, i am using the following dependency for my project But i am getting org.powermock.api.mockito.expectation.WithOrWithoutExpectedArguments not found please look on the dependency, 1.8
Santosh
  • 111
  • 2
  • 10
5
votes
2 answers

How to combine PowerMock and Robolectric

I have a static method that checks for internet status, that uses a Context. So to mock it i need to use PowerMock. I am using robolectric to speed up my test since the only thing i need mocked is the mocked. But i cant seem to configure…
Zeyad Gasser
  • 1,516
  • 21
  • 39
5
votes
1 answer

Always get thread error even if unit test passes in Android using Mockito

I always get an error even if a test passes and not sure why. This time i was checking there is a null pointer if the view is null. @Before public void setUp() { MockitoAnnotations.initMocks(this); mockView =…
Gabi Radu
  • 1,107
  • 2
  • 16
  • 35
5
votes
4 answers

Mock android Patterns with mockito

I want validate an email with some code provided by Android. Here is the code I want to mock : if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()) throw new InvalidPhoneException(phone); In my test file…
Xero
  • 3,951
  • 4
  • 41
  • 73
5
votes
2 answers

why Mock object has doreturn and thenreturn for mock?

Note: I understand that in spy we can differenciate among these two. I went all over internet but I still some doubts on doreturn/when and when/thenreturn in Mockito .Below is my doubt, 1)Are doreturn/when and when/thenreturn acts same for mock…
Avinash Jethy
  • 803
  • 2
  • 10
  • 25
5
votes
2 answers

Mocking/stubbing private variables of a class without getter and setter methods

I recently started working on TDD for exisitng project and faced couple of issues, one of those is mentioned below I have a private variable to be mocked in test class and the variable looks like below private Class cls = XYZ.class; later this…
Praveen Kumar Mekala
  • 628
  • 1
  • 10
  • 26
5
votes
1 answer

PowerMockito is calling the method when I use doReturn(..).when(....)

I'm new to PowerMockito and there's a behavior it's displaying which I don't understand. The following code explains my issue: public class ClassOfInterest { private Object methodIWantToMock(String x) { String y = x.trim(); //Do some…
Ahmad
  • 12,886
  • 30
  • 93
  • 146
5
votes
1 answer

Verify call to static method

I want to verify that a public static void method has been called. @RunWith(PowerMockRunner.class) @PrepareForTest({ConsoleLog.class}) public class AdContentDataUnitTest { @Before public void setUp() throws Exception { …
Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119
5
votes
2 answers

Mockito/Power Mockito : unable to get expected output when mocking method of LayoutParams in android

I have a method: public class MarginConverter { int top = 0; int bottom = 0; int right = 0; int left = 0; public MarginConverter(String val){ top = bottom = right = left = Integer.parseInt(val); } public…
Anuja Kothekar
  • 2,537
  • 2
  • 15
  • 28
5
votes
3 answers

PowerMockito NullPointerException when stub

I want to stub the public static function readAllBytes from java.nio.file.Files with the following test code. @PrepareForTest(Files.class) public void testGetNotExistingRestFile() throws Exception { PowerMockito.mockStatic(Files.class); …
bmooij
  • 142
  • 2
  • 9
4
votes
2 answers

How to mock a return from a call to a private method

I have a class which has a method I want to test, but it calls a private method in the same class to get a Map value. I want to mock what the private method returns to the method I want to test. import java.util.*; public class ClassOne { …
Shakakhan
  • 41
  • 1
  • 1
  • 2
4
votes
2 answers

Using PowerMock in JDK 16

Java reflection is becoming more and more restricted: Up to Java 8 all operations are allowed Starting from Java 9 to 15 you are still able to perform the operations, but you will receive a warning From Java 16 and onwards the operations are…
rigon
  • 1,310
  • 4
  • 15
  • 37
4
votes
1 answer

Override DNS lookup using mockito during junit java test

Is it possible to override a single DNS lookup in the java jvm? I am running a junit test in java. The test makes a connection to an external server host1. Instead, I want the jvm to contact localhost when it tries to contact host1. The test…
user674669
  • 10,681
  • 15
  • 72
  • 105
4
votes
0 answers

integration test with SpringBoot with both MockMvc and PowerMock on static methods

Can someone provide any sample on this scenario combination. I gave a sample snippet on the lines below, which is failing while trying to mock, intent is to combine both MockMVC with PowerMock Create a spring Boot Test App for integration testing…
4
votes
2 answers

NoClassDefFoundError or NoSuchMethodError when trying to mock static (incompatible dependencies)

When a class with static method is mocked, an exception been thrown. The version 2.0.0 of PowerMock displays NoClassDefFoundError1. The versions 2.0.1 and 2.0.2 displays NoSuchMethodError2. In project that I work is used Mockito on version 2.25.0,…
rogerio_gentil
  • 359
  • 1
  • 4
  • 17