Having problem with params capturing in junit test.
I have the method which send person object to another service. Also I use circuitBreaker to continue app run, even if person service is not available.
Circuit-Breaker bean configuration:
@Bean
public CircuitBreaker circuitBreaker(CircuitBreakerFactory<Resilience4JCircuitBreakerConfiguration, Resilience4JConfigBuilder> factory) {
return factory.create("circuitBreaker");
}
@Autowired
private PersonClient personClient;
@Autowired
private CircuitBreaker circuitBreaker;
public void save(Person person) {
//setting some fields into person.
circuitBreaker.run(() -> personClient.sendPerson(person), throwable -> {
log.error("Error sending message to PersonService: ", throwable);
return null;
});
}
}
I am having trouble testing this method. Here is my test:
@InjectMocks
private CustomerServiceImpl customerService;
@Mock
private PersonClient personClient;
@Captor
private ArgumentCaptor<Person> captor;
@Mock
private CircuitBreaker circuitBreaker;
@Test
void savePerson() {
//person obj created here;
when(personClient.sendPerson(any(Person.class))).thenReturn(Response.class);
customerService.save(obj);
verify(personClient).sendPerson(captor.capture()); // failing here
verifyNoMoreInteractions(personClient);
var testPerson = captor.getValue();
//assertions
Here is fail description: Wanted but not invoked:personClient.sendPerson();
Is there a way to capture argument here? I captured it without having circuitBreaker, but with circuitBreaker (call the service 3 times, otherwise return nothing) having fail test.