Say I have a repository interface looks like this,
@Repository
interface MyRepository {
Optional<My> findByOtherId(long otherId);
default Optional<My> findByOther(Other other) {
return findByOtherId(other.getId());
}
}
I'm trying to invoke findByOther
and verifies that the call invokes findByOtherId
method.
@DataJpaTest
class MyRepositoryTest {
@Test
void test() {
Other other = new Other(0L);
repository.findByOther(other);
verify(other, times(1)).getId(); // verified
verify(repository, times(1)).findByOtherId(other.getId()); // called, yet not verified!
}
@SpyBean
private MyRepository repository;
}
When I debug, the findByOtherId
method is called. But mockito complains it doesn't.
How can I do this?