I have some logic quite similar to this, where i have unit and different fields that can be updated during request.
public class Unit {
int x;
int y;
public void updateX(int x) {
this.x += x;
}
public void updateY(int y) {
this.y += y;
}
}
public class UpdateUnitService{
public Unit update(int delta, BiConsumer<Unit, Integer> updater){
Unit unit = getUnit(); //method that can`t be mocked
updater.accept(unit, delta);
// some save logic
return unit;
}
}
public class ActionHandler{
private UpdateUnitService service;
public Unit update(Request request){
if (request.someFlag()){
return service.update(request.data, Unit::updateX);
}else {
return service.update(request.data, Unit::updateY);
}
}
}
And I need write some test for checking what function was called. Something like this.
verify(service).update(10, Unit::updateX);
verify(service).update(10, Unit::updateY);
How to write test like this using ArgumentCaptor or something else?