javamockingmockitoannotationspowermock

org.jetbrains.annotations @NotNull interfare with Mockito when mock a private method


I'm trying to mock a private method of my service with powermock by passing it the mockito matcher "eq" for its arguments. One of those argument is annotated with the @NotNull decorator from jetbrains.

When I run the test, I get the following error:

java.lang.IllegalArgumentException: Argument for @NotNull parameter X of Y must not be null

Target for testing

import org.jetbrains.annotations.NotNull;
...

class Service {
...
  private Map<String, Object> computeDataModel(User user, @NotNull Configuration configuration, Object providedDataModel) {...
}

Test Class

PowerMockito.when(service, "computeDataModel", eq(user), eq(configuration), eq(providedDataModel)).thenReturn(dataModel);

I also tried with any(Configuration.class) but without success.

Do you know how to process ? Thank you for your attention


Solution

  • This has nothing to do with the annotation really, but with calling the real method with null values.

    PowerMockito.when(service, "computeDataModel", eq(user), eq(configuration), eq(providedDataModel))
        .thenReturn(dataModel);
    

    calls the real method.

    eq is a matcher and returns null, effectively calling your method with 3 null arguments.

    If you want to avoid calling your real method, you could use a different method of PowerMockito:

    PowerMockito.doReturn(dataModel)
        .when(service, "computeDataModel", eq(user), eq(configuration), eq(providedDataModel));