Is it possible to mock Mockito accessories in Kotlin?

Is it possible to mock the recipient and installer of the Mockito property? Something like that:

@Test fun three() { val m = mock<Ddd>() { // on { getQq() }.doReturn("mocked!") } assertEquals("mocked!", m.qq) } open class Ddd { var qq : String = "start" set(value) { field = value + " by setter" } get() { return field + " by getter" } } 
+13
source share
2 answers

To mock a recipient, simply write:

 val m = mock<Ddd>() `when`(m.qq).thenReturn("42") 

I also suggest using mockito-kotlin to use useful extensions and functions like whenever :

 val m = mock<Ddd>() whenever(m.qq).thenReturn("42") 
+12
source

Complementing the IRus answer, you can also use the following syntax:

 val mockedObj = mock<SomeClass> { on { funA() } doReturn "valA" on { funB() } doReturn "valB" } 

or

 val mockedObj = mock<SomeClass> { on(it.funA()).thenReturn("valA") on(it.funB()).thenReturn("valB") } 
+6
source

Source: https://habr.com/ru/post/1259718/


All Articles