I'm newbie on Kotlin, Mockito. Below code has an error which is caused by engine
, an immutable field that is not mocked.
I spent a lot of time trying to solve this problem. I found that a way of mocking works, until I don't understand and don't satisfy that.
- move a field to constructor.
class Car(val engine: Engine) { ... }
- change to mutable field.
private var engine = Engine()
Is there any other way?
class Car {
private val engine = Engine() // here
var state: String? = null
fun move() {
state = engine.state
}
}
@RunWith(MockitoJUnitRunner::class)
class CarTest {
@Mock private lateinit var mockedEngine: Engine
@InjectMocks private val car = Car()
@Test
fun test() {
`when`.(mockedEngine.state).thenReturn("run")
car.move()
assertEquals("run", car.state)
}
}