简体   繁体   中英

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?

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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM