Kotlin Cheatsheet
Testing & Gradle
Use this Kotlin reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Gradle Kotlin DSL (build.gradle.kts)
plugins {
kotlin("jvm") version "2.2.0" // Kotlin 2.x = K2 compiler (stable since 2024)
application
}
repositories { mavenCentral() }
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2")
testImplementation(kotlin("test")) // kotlin.test + JUnit 5 wiring
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2")
}
kotlin {
jvmToolchain(21) // pins JDK for compile + test
compilerOptions {
allWarningsAsErrors.set(true)
}
}
tasks.test { useJUnitPlatform() } // JUnit 5 runner
application { mainClass.set("demo.MainKt") }./gradlew build # compile + test ./gradlew test --tests "demo.SolverTest" ./gradlew run ./gradlew dependencies --configuration runtimeClasspath
Version catalogs (gradle/libs.versions.toml) centralize versions: declare kotlin = "2.2.0" under [versions], then alias(libs.plugins.kotlin.jvm) in the plugins block.
kotlin.test basics
import kotlin.test.* class SolverTest { @Test fun `adds two numbers`() { // backtick names read well in reports assertEquals(5, add(2, 3)) } @Test fun failures() { val e = assertFailsWith<IllegalArgumentException> { parse("") } assertTrue("blank" in e.message.orEmpty()) } @BeforeTest fun setUp() { } @AfterTest fun tearDown() { } }
| Assertion | Checks |
|---|---|
assertEquals(exp, act) / assertNotEquals | structural equality |
assertTrue { cond } / assertFalse | boolean |
assertNull(x) / assertNotNull(x) | nullability (assertNotNull returns non-null) |
assertContains(coll, item) | membership (also strings) |
assertContentEquals(a, b) | arrays / iterables element-wise |
assertIs<T>(x) | type check + smart cast |
assertFailsWith<E> { } | expected exception, returns it |
fail("msg") | explicit failure |
kotlin.test is a facade — on the JVM it delegates to JUnit 5 (with useJUnitPlatform()). Mixing in raw JUnit 5 (@ParameterizedTest, @Nested) or libraries like Kotest/MockK is normal.
Testing coroutines
import kotlinx.coroutines.test.runTest import kotlin.test.* class RepoTest { @Test fun loadsUser() = runTest { // virtual time: delay() skips instantly val repo = Repo() assertEquals("user-1", repo.fetchUser(1)) } }
runTest gives a TestScope with virtual time (advanceTimeBy(1_000), runCurrent()); inject Dispatchers into production code so tests can substitute a StandardTestDispatcher.
Parameterized & data-driven
// JUnit 5 params: testImplementation("org.junit.jupiter:junit-jupiter-params") import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.CsvSource class FizzTest { @ParameterizedTest @CsvSource("3,Fizz", "5,Buzz", "15,FizzBuzz") fun fizz(n: Int, expected: String) = assertEquals(expected, fizzbuzz(n)) } // or zero-dependency table style: @Test fun table() { listOf(3 to "Fizz", 5 to "Buzz").forEach { (n, want) -> assertEquals(want, fizzbuzz(n), "n=$n") } }
Compiler CLI (no Gradle)
kotlinc Main.kt -include-runtime -d main.jar && java -jar main.jar kotlinc src/ -d out/ # compile a tree kotlin -e 'println(1 + 2)' # one-liner eval