跳至主要内容

在 JVM 中使用 JUnit 測試程式碼 – 教學

本教學將示範如何在 Kotlin/JVM 專案中編寫一個簡單的單元測試,並使用 Gradle 建置工具來執行它。

在這個專案中,您將使用 kotlin.test 函式庫,並使用 JUnit 執行測試。 如果您正在開發一個多平台應用程式,請參閱 Kotlin Multiplatform 教學

首先,下載並安裝最新版本的 IntelliJ IDEA 以開始。

新增依賴項 (dependencies)

  1. 在 IntelliJ IDEA 中開啟一個 Kotlin 專案。如果您沒有專案, 建立一個

  2. 開啟 build.gradle(.kts) 檔案,並檢查是否存在 testImplementation 依賴項。 這個依賴項讓您可以使用 kotlin.testJUnit

    dependencies {
    // Other dependencies.
    testImplementation(kotlin("test"))
    }
  3. test 任務 (task) 新增到 build.gradle(.kts) 檔案中:

    tasks.test {
    useJUnitPlatform()
    }

    如果您在建置腳本中使用 useJUnitPlatform() 函數 (function), 則 kotlin-test 函式庫會自動包含 JUnit 5 作為依賴項。 這種設定可以在純 JVM 專案和 Kotlin Multiplatform (KMP) 專案的 JVM 測試中, 存取所有 JUnit 5 API,以及 kotlin-test API。

以下是 build.gradle.kts 的完整程式碼:

plugins {
kotlin("jvm") version "2.1.20"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}

dependencies {
testImplementation(kotlin("test"))
}

tasks.test {
useJUnitPlatform()
}

新增要測試的程式碼

  1. src/main/kotlin 中開啟 Main.kt 檔案。

    src 目錄包含 Kotlin 原始碼檔案和資源。 Main.kt 檔案包含範例程式碼,用於印出 Hello, World!

  2. 建立包含 sum() 函數的 Sample 類別 (class),該函數將兩個整數相加:

    class Sample() {

    fun sum(a: Int, b: Int): Int {
    return a + b
    }
    }

建立測試

  1. 在 IntelliJ IDEA 中,針對 Sample 類別選擇 Code | Generate | Test...

    Create a test
  2. 指定測試類別的名稱。 例如,SampleTest

    Create a test

    IntelliJ IDEA 會在 test 目錄中建立 SampleTest.kt 檔案。 此目錄包含 Kotlin 測試原始碼檔案和資源。

    您也可以在 src/test/kotlin 中手動建立用於測試的 *.kt 檔案。

  3. SampleTest.kt 中新增 sum() 函數的測試程式碼:

    • 使用 @Test 注釋 定義測試 testSum() 函數。
    • 透過使用 assertEquals() 函數,檢查 sum() 函數是否傳回預期的值。
    import org.example.Sample
    import org.junit.jupiter.api.Assertions.*
    import kotlin.test.Test

    class SampleTest {

    private val testSample: Sample = Sample()

    @Test
    fun testSum() {
    val expected = 42
    assertEquals(expected, testSample.sum(40, 2))
    }
    }

執行測試

  1. 使用邊溝圖示 (gutter icon) 執行測試:

    Run the test

    您也可以使用 ./gradlew check 命令,透過命令列介面執行所有專案測試。

  2. Run 工具視窗中檢查結果:

    Check the test result. The test passed successfully

    測試函數已成功執行。

  3. 透過將 expected 變數值變更為 43,確保測試可以正常運作:

    @Test
    fun testSum() {
    val expected = 43
    assertEquals(expected, classForTesting.sum(40, 2))
    }
  4. 再次執行測試並檢查結果:

    Check the test result. The test has failed

    測試執行失敗。

後續步驟

完成第一個測試後,您可以: