How do you test a method that calls another method...
# android
n
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
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
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
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
Thanks !! That is what I am looking for
Just a quick question @Raul Tunduc, is this possible with mockk, not mockito
r
I don’t know. Check their documentation.