https://kotlinlang.org logo
#mockk
Title
# mockk
m

mcpiroman

04/09/2022, 6:39 PM
I have a function with many overloads:
Copy code
fun prepareStatement(sql: String): PreparedStatement
fun prepareStatement(sql: String, resultSetType: Int): PreparedStatement
fun prepareStatement(sql: String, resultSetType: Int, resultSetConcurrency: Int): PreparedStatement
fun prepareStatement(sql: String, resultSetType: Int, resultSetConcurrency: Int, resultSetHoldability: Int): PreparedStatement
What if I want to mock all of them in the same way, whereas I only care about the `sql`parameter? Currently I repeat
every  {
block for each overload. Is there more preferred way, e.g. matching functions by name?
m

Mattia Tommasone

04/10/2022, 8:07 AM
i’m guessing (hoping, really) your overloads each call the last one setting default values for the parameters that they don’t receive. If that’s the case, you should be able to have the first overloads mocked with
every { prepareStatement(any()) } answers { callOriginal() }
and then mock just the last one in each of your tests does this make sense to you?
m

mcpiroman

04/11/2022, 9:04 AM
Thanks for the idea, I have not thought that the overloads should actually call each other. But I have already extracted the answer to a `val answer = FunctionAnswer { call -> ... }`and provided that to each overload, so if I need to specify them all anyway, then that way is very close.
m

Mattia Tommasone

04/11/2022, 9:07 AM
makes a lot of sense