Bruno Miguel
Bruno Miguel

Reputation: 1115

PowerMockito.verifyPrivate always working independently of parameters

As the title states, I can call verifyPrivate but it always gives me success even if I pass the wrong parameters to it.

Real example

@RunWith(PowerMockRunner.class)
@PrepareForTest({MyService.class})
public class MyServiceTest {

@InjectMocks
MyService service;

@Mock
OperationSivRepo operationSivRepo;

@Test
public void getNbInscriptions_should_call_getNbOperationsSiv_with_OPERATION_INSCRIPTION_GAGE() throws Exception {
    // GIVEN
    Role role = Role.ADMINISTRATEUR;
    String operator = "operator";
    SivDto sivDto = new SivDto();

    // WHEN
    service.getNbInscriptions(operator, role, sivDto);

    // THEN
    verifyPrivate(service).invoke("privateMethod", operator, Role.ADMINISTRATEUR, sivDto);
  }
}

Now this code will succeed, even if I do something like

// THEN
verifyPrivate(service).invoke("privateMethod", "other string", Role.USER, new SivDto());

Maybe I'm missing something but I just can't figure it out.

Upvotes: 1

Views: 425

Answers (1)

pvpkiran
pvpkiran

Reputation: 27068

Firstly. Did you put a debug point in privateMethod and see how many times it is getting called? This would have given you some hint.
It is getting called two times. Once when you call

service.getNbInscriptions(operator, role, sivDto);

and once when you use

verifyPrivate(service).invoke("privateMethod", operator, Role.ADMINISTRATEUR, sivDto);

Second time since it is getting called with the arguments you passed to invoke method, the tests always succeed.

Use Spy instead of Mock Instead of

@InjectMocks
MyService service;

Use

@Spy
MyService myservice = new MyService(operationSivRepo)

Wiht this, second invocation to the method is not made and arguments are verified properly.

Upvotes: 1

Related Questions