Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powermockito doNothing for method with arguments

I've developed an application in Java and I'm trying to create unit tests using Powermockito (I should add that I'm new to unit testing).

I have a class called Resource which has a static method called readResources:

public static void readResources(ResourcesElement resourcesElement);

ResourcesElement is also coded by me. In testing, I want to create my own Resource, so I want the above method to do nothing. I tried using this code:

    PowerMockito.spy(Resource.class);
    PowerMockito.doNothing().when(Resource.class, "readResources", Matchers.any(ResourcesElement.class));

The unit test throws an exception:

org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: -> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36)

Powermockito also suggest that I should use thenReturn or thenThrow after when, but it seems that the method 'when' returns void when it is called after doNothing (which is logical). If I try:

PowerMockito.when(Resource.class, "readResources", Matchers.any(ResourcesElement.class)).....

doNothing is not an option after when.

I managed to make methods without arguments to do nothing, using the 2 arguments version of the method. For example:

PowerMockito.doNothing().when(Moduler.class, "startProcessing");

This works (startProcessing doesn't take any arguments).

But how can I make methods that do take arguments to do nothing with Powermockito?

like image 666
Anakin001 Avatar asked Apr 10 '14 07:04

Anakin001


People also ask

How do you mock a private void method?

For Mockito, there is no direct support to mock private and static methods. In order to test private methods, you will need to refactor the code to change the access to protected (or package) and you will have to avoid static/final methods.

How do I use Powermock with JUnit 5?

To include powermock in our application, add the powermock-api-mockito2 and powermock-module-junit4 dependencies. Note that there is no official extension for JUnit 5. If you plan to use its reflection module, for example invoking the private methods, then we need to import powermock-reflect module as well.

What is PowerMockito used for?

PowerMockito is a PowerMock's extension API to support Mockito. It provides capabilities to work with the Java Reflection API in a simple way to overcome the problems of Mockito, such as the lack of ability to mock final, static or private methods.

How do you verify a static method is called in PowerMockito?

verifyStatic(VerificationModeFactory. times(2)) which tells PowerMock to verify static method was called 2 times. The second part is Utils. randomDistance(1) which tells exactly which static method should be verified.


Video Answer


2 Answers

You can find a fully functional example below. Since you didn't post the complete example, I can only assume that you did not annotate the test class with @RunWith or @PrepareForTest because the rest seems fine.

@RunWith(PowerMockRunner.class)
@PrepareForTest({Resource.class})
public class MockingTest{

    @Test
    public void shouldMockVoidStaticMethod() throws Exception {
        PowerMockito.spy(Resource.class);
        PowerMockito.doNothing().when(Resource.class, "readResources", Mockito.any(String.class));

        //no exception heeeeere!
        Resource.readResources("whatever");

        PowerMockito.verifyStatic();
        Resource.readResources("whatever");
    }

}

class Resource {
    public static void readResources(String someArgument) {
        throw new UnsupportedOperationException("meh!");
    }
}
like image 116
Morfic Avatar answered Dec 13 '22 04:12

Morfic


Why go through so much trouble just so that your method does not do anything. Just calling PowerMockito.mockStatic(Resource.class) should replace all static methods in your class with default stubs which basically mean they do nothing.

Unless you do want to change the behavior of your method to actually do something just calling PowerMockito.mockStatic(Resource.class) should suffice. Ofcourse this also means all static methods in the class are stubbed which you need to consider.

like image 37
Aniket Thakur Avatar answered Dec 13 '22 05:12

Aniket Thakur