Kotlin tests typically use JUnit 5 or JUnit 4 with Gradle's test task. Assertions from kotlin.test or AssertJ/Kotest run on the JVM like Java tests—plus coroutine test dispatchers locally.
Basic test shape
class GreeterTest {
@Test
fun greets() {
assertEquals("Hello, Ada", greet("Ada"))
}
}
Place tests under src/test/kotlin. Use descriptive names; prefer one logical assertion per behavior.
Important interview questions and answers
- Q: kotlin.test vs JUnit?
A:kotlin.testoffers multiplatform assertions; JVM projects still rely on JUnit runners and Gradle integration. - Q: Testing coroutines?
A: UserunTestfrom kotlinx-coroutines-test with test dispatchers—avoid real delays.
Self-check
- Where do Gradle JVM tests live?
- What Gradle task runs tests?
Tip: Use runTest from coroutines-test for suspend functions—plain JUnit alone is not enough.
Interview prep
- JUnit with Kotlin?
@Test on functions; kotlin-test assertions optional.
- runTest?
Test coroutines with virtual time from coroutines-test.