Skip to main content

Spying

To create a spy use spy with spied object as a parameter.

warning

Please read limitations section!

val repository = BookRepositoryImpl()
val spiedRepository = spy<BookRepository>(repository)

Ensure that you specify a type for the spy. In previous example inferred type would be BookRepositoryImpl, which is very likely to be a final class, and it is not supported to spy them!

By default, spy delegates all calls to spied object. However, you can change methods behaviour in the same way as with mocks.

Read verifying section to learn how to verify calls on spy!