October 2019
Intermediate to advanced
434 pages
11h 54m
English
To demonstrate the challenge associated with testing closed classes in Kotlin, we can walk through a simple, but common, example. In this code snippet, we've defined a class, SimpleGreeter, that implements the GreetingProvider interface:
class SimpleGreeter() : GreetingProvider { override fun getGreeting(): String { return "Hi" }}
Now, we can try to mock this SimpleGreeter class with Mockito:
val mockProvider = Mockito.mock(SimpleGreeter::class.java) // Runtime error
However, mocking SimpleGreeter in this way will result in an error. That error will look something like this:
org.mockito.exceptions.base.MockitoException: Cannot mock/spy class SimpleGreeterMockito cannot mock/spy because : - final class
Essentially, what the error ...
Read now
Unlock full access