Код: Выделить всё
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class com.example.outsideintddexample.Engine.
Can not mock final classes with the following settings :
- explicit serialization (e.g. withSettings().serializable())
- extra interfaces (e.g. withSettings().extraInterfaces(...))
Код: Выделить всё
package com.example.outsideintddexample
import android.util.Log
import kotlinx.coroutines.delay
class Engine(
var temperature: Int = 15,
var isTurnedOn: Boolean = false
) {
suspend fun turnOn() {
isTurnedOn = true
delay(5000)
Log.d("Teste", "Hello World")
temperature = 95
}
}
Код: Выделить всё
class CarShould {
private val engine: Engine = mock()
private val car = Car(engine,5.0)
@get:Rule
var rule = MainCoroutineScopeRule()
@Test
fun looseFuelWhenItTurnsOn() = runBlockingTest {
car.turnOn()
assertEquals(4.5, car.fuel)
}
@Test
fun turnOnItsEngine() = runBlockingTest {
car.turnOn()
verify(engine, times(1)).turnOn()
}
Я использую testImplementation 'com. nhaarman.mockitokotlin2:mockito-kotlin:2.1.0' и testImplementation 'org.mockito:mockito-inline:2.21.0'
Подробнее здесь: https://stackoverflow.com/questions/656 ... this-class