-1

as the title suggests, the problem is this: I have the mock of AdesioneMerger and I have to call the real merge method. merge is a void method.

This is wrong:

adesioneMerger = Mockito.spy(AdesioneMerger.class);

Mockito.when(adesioneMerger.merge(
    Matchers.any(AdesioneBean.class),
    Matchers.any(Adesione.class), 
    Matchers.any(ServiceResultBean.class))
).ThenCallRealMethod();

what's the error?

Paul Benn
  • 1,911
  • 11
  • 26

2 Answers2

0

For void methods, use the alternative API:

Mockito.doCallRealMethod()
  .when(adesioneMerger).merge(
     Matchers.any(AdesioneBean.class),
     Matchers.any(Adesione.class), 
     Matchers.any(ServiceResultBean.class));

The problem is that Mockito.when() requires an argument (and there are no void-typed arguments in Java). The alternative API works around this by calling when() on the mock type and the actual method to mock is called on the return value of when(...).

See also this answer: How to make mock to void methods with mockito

Stefan Winkler
  • 3,871
  • 1
  • 18
  • 35
0

try this way:

doCallRealMethod().when(adesioneMerger).merge(
    Matchers.any(AdesioneBean.class),
    Matchers.any(Adesione.class), 
    Matchers.any(ServiceResultBean.class);
Selindek
  • 3,269
  • 1
  • 18
  • 25