Skip to content

Instantly share code, notes, and snippets.

View OliverCulleyDeLange's full-sized avatar

Oliver Culley de Lange OliverCulleyDeLange

View GitHub Profile
@OliverCulleyDeLange
OliverCulleyDeLange / README.md
Last active August 2, 2023 14:29
Test LiveData with KoTest and Mockk

Testing ViewModel / LiveData with KoTest (previously Kotlintest) and Mockk

I went round a few houses trying to figure out how best to test ViewModels containing LiveData using Kotlin based testing tools. My main mistakes were trying to use JUnit 5 annotations with KoTest test classes and expecting them to work. The @ExtendWith() annotation is from the org.junit.jupiter.api.extension package. Jupiter is the module used for writing tests, whereas KoTest runs on the JUnit 5 Platform. Very different thing apparently

I came accross this Mockk issue which pointed me towards this write up on how to test live data with JUnit 5. This gave me the code to delegate the live data executor to a custom one which simply executes immedietely.

I've not tested thi