user3537932
user3537932

Reputation: 163

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 static String methodToMock(String item){
      return "asdf";    
    }
}

For whatever reason, though, running the following:

PowerMockito.spy(asdfClass.class);

PowerMockito.when(asdfClass.methodToMock(Mockito.any())).thenReturn("asdfghj");

appears to compile correctly but running

PowerMockito.spy(asdfClass.class);

PowerMockito.when(asdfClass.methodToMock(Mockito.any(), Mockito.any())).thenReturn("asdfghj");

does not and spits out a "'void' type not allowed here" error on the Mockito.any()s.

Does anyone know what to do about this? The only other result I saw on stackoverflow suggested that the reader take a look at the documentation, though I don't think it said anything about multiple arguments in a private static method.

(In the end I'm hoping to mock a void result with a doNothing though I've boiled the issue I'm having down to the fact that all of my void methods take multiple arguments)

EDIT: Never mind, got it: Is it possible to use partial mocking for private static methods in PowerMock? (Comment 4 on the chosen answer). Curiously this didn't work before but that might've been a typo on my part for all I know)

Upvotes: 3

Views: 5585

Answers (2)

user3537932
user3537932

Reputation: 163

As per Is it possible to use partial mocking for private static methods in PowerMock?, PowerMockito.doReturn(mockData).when(DataProvider.class, "readFile", param1, param2, ...) does the trick.

Upvotes: 4

Rafał P
Rafał P

Reputation: 252

You mock void methods, so it can't return anything, so the thenReturn() statement should be omitted (for example instead of when(), use doNothing()).

Upvotes: 1

Related Questions