Duncan McGregor
Duncan McGregor

Reputation: 18167

How do I mock a Kotlin function type returning a value class with Mockk?

I have a test subject that takes a function type in its constructor:

class PricedStockListLoader(
    val stock: Stock,
    val pricing: (Item) -> Price?
)

In my test I can mock both dependencies:

val stock: Stock = mockk()
val pricing: (Item) -> Price? = mockk()
val loader = PricedStockListLoader(stock, pricing)

and specify expectations on the stock

every { stock.stockList(sameDayAsLastModified) } returns Success(stockList)

but if I try to specify an expectation on pricing

every { pricing.invoke(anItem) } returns Price(666)

that line (the one specifying the expectation) never returns, stuck on an Object.wait somewhere inside JUnit.

Can I mock function types returning value classes?

Upvotes: 3

Views: 3215

Answers (1)

Ruslan
Ruslan

Reputation: 14630

Issue related to mocking inline classes.

Minimal, Reproducible Example:

@JvmInline
value class Foo(val bar: String)

val f: () -> Foo = mockk()

@Test
fun `test value`() {
    every { f.invoke() } returns Foo("test")
    f.invoke()
}

Upvotes: 2

Related Questions