https://kotlinlang.org logo
#android
Title
# android
n

Nikola Milovic

02/24/2020, 1:41 PM
How do you test a method that calls another method with Junit5 and mockk? I tried googling but no luck. The SUT has a method that after some logic, calls other method. How can i verify that the method was called without calling it? As I want to keep it a unit test. Can i mock the method somehow?
r

Raul Tunduc

02/24/2020, 1:42 PM
yes, you can mock that method to with
when
and
then
for example:
Copy code
val mockBookService = Mockito.mock(BookService::class.java)
Mockito.`when`(mockBookService. inStock(100)).thenReturn(true)
n

Nikola Milovic

02/24/2020, 1:46 PM
Okay, but for example
Copy code
fun a() {
If(logic) {b()}
}
}
The both methods are in the SUT which is not mocked The b() contains logic and dont want to execute it just to confirm its called
Fuck on mobile couldnt make code block😂 sorry
r

Raul Tunduc

02/24/2020, 1:48 PM
yes. Before calling function
a()
you can write smth like this:
Copy code
Mockito.`when`(b()).thenReturn(true)
and that’s all. That method will not be executed, only it will return true 🤷‍♂️
n

Nikola Milovic

02/24/2020, 1:48 PM
Thanks !! That is what I am looking for
Just a quick question @Raul Tunduc, is this possible with mockk, not mockito
r

Raul Tunduc

02/24/2020, 3:15 PM
I don’t know. Check their documentation.
6 Views