Kotlin·Kotlin from syntax to a coroutine app

Testing

Test pure logic with JUnit and coroutines with runTest.

Beginner 13 min read Lesson 8 of 300 Challenge included
What you will be able to do
  • Test pure functions without Android.
  • Use runTest for suspend functions.
  • Use descriptive test names.
  • Test the failure path too.

In plain English

Keep logic in pure functions so tests stay fast. Use JUnit assertions and kotlinx-coroutines-test for suspend code. Test behaviour, not implementation details.

LessonTest.ktKotlin
import kotlin.test.Test
import kotlin.test.assertEquals

class LessonTest {
    @Test
    fun classifiesShortLessons() {
        assertEquals("short", classify(9))
    }
}
Your turn

Assert equality

Easy

Write a test assertion that classify(9) equals "short".

KOTLIN · starter
assertEquals()
Check your understanding before moving on.
Knowledge checkQuestion 1

Which helper runs suspend functions in tests?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 8 of 300 · 0% of course