I have a following class:
public class Task {
public <V> V run(Callable<V> callable) {
//...
callable.call()
}
}
And I have a class that uses the above:
public MyClass {
Task task;
Provider provider;
public void doYourJob() {
String parameter = "Test";
task.run(() -> provider.save(parameter));
}
}
I need to now write unit test to this piece, so that it checks if I actually called provider with proper parameters.
I tried approach with argumentCaptors:
@ExtendWith(MockitoExtension.class)
class MyClassTest {
@Mock
Task task;
@Mock
Provider provider;
MyClass myClass;
@Captor
ArgumentCaptor<Callable<String>> captor;
@BeforeEach
public void setup()
myClass = new MyClass(task, provider);
}
@Test
void shouldDoJob() {
myClass.doYourJob();
verify(task).run(captor.capture());
captor.call();
verify(provider).save("Test");
}
}
This fails with error: Wanted but not invoked:
task.run(<Capturing argument));
There was an interaction with this mock:
task.run(MyClass$$Lambda$$598dsfg897);
Also I tried approach with mocking and with doAnswer - in all cases Callable parameter was interpreted wrong.
Any clues how to mock Callable and check what was actually called?