Thommy
Thommy

Reputation: 5407

Kotlin: Access private var in test

I have a Kotlin class containing the following property

private var items: List<Item> = listOf()

In my test I now want to check if there are items in the list. I try to do this with reflection:

val field = sut::class.members.findLast { f -> f.name.equals("items") }

I now get a KCallable<*> back but I don't know how I can access or at least count the items in my list.

Upvotes: 0

Views: 702

Answers (1)

yole
yole

Reputation: 97168

The KCallable you get is actually an instance of KProperty1, and you can call its get method to get the instance:

 val items = (field as KProperty1<ClassUnderTest, List<Item>>).get(sut)

Upvotes: 1

Related Questions