I want to use a Supplier interface in my tests. I have mocked it right now as a crude soln, but how do i define it ? In Java I would do something like ,
Supplier<List<String>> list = () -> Collections.emptyList();
is there a way to define it like this in kotlin as well?
In Kotlin, functional objects are first class so you don't need Supplier to create a function that produces something unless you're working with a Java API that demands it. The functional equivalent of a Supplier would be a function that has no inputs and returns something, so you'd define it like this:
val list: ()->List<String> = { emptyList() }
If you need a literal Supplier, you can use the constructor-like "adapter function" SAM-conversion lambda syntax:
val list: Supplier<List<String>> = Supplier { emptyList() }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With