Skip to content

使用 Maven 測試 Kotlin 專案

Kotlin 與 Maven 生態系統無縫整合,讓您可以使用業界標準工具來驗證您的後端應用程式。在本指南中,您將學習如何使用 JUnit 建立測試,並使用 Maven 外掛程式執行單元測試與整合測試。

如需設定 Maven 專案以同時使用 Kotlin 與 Java 的詳細指南,請參閱

使用 JUnit 建立測試

JUnit 是 Kotlin 後端開發的標準測試架構。雖然 Kotlin 支援多個 JUnit 版本,但大多數現代專案應使用 JUnit 6。

若要使用 JUnit 在 Kotlin 中建立測試,請使用來自 kotlin.test 或 JUnit 套件的 @Test 註解。

新增相依性

kotlin-test 程式庫是開始測試最簡單的方式。它提供了一組通用的斷言,並會自動拉取必要的 JUnit 構件。

JUnit 5 及更高版本

對於所有新專案,請使用 kotlin-test-junit5 構件。它提供對 JUnit 的完整支援,包括巢狀測試和平行執行等功能。Kotlin/JVM 支援最新的穩定 JUnit 版本:JUnit 6。

請按如下方式更新您的 pom.xml 檔案:

xml
<dependencies>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-test-junit5</artifactId>
        <version>2.3.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

儘管名稱如此,kotlin-test-junit5 仍支援所有最新的 JUnit 版本,包括 JUnit 6。

JUnit 4

如果您想使用較早版本的 JUnit(例如用於舊版專案),請使用利用 JUnit 4 的 kotlin-test-junit 構件:

xml
<!-- pom.xml -->
<dependencies>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-test-junit</artifactId>
        <version>2.3.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

有關使用 JUnit 進行測試的詳細指南和範例專案,請參閱使用 Kotlin 測試 Java 程式碼教學。

撰寫單元測試

單元測試驗證程式碼中孤立的部分,例如個別函式或類別。 依照慣例,單元測試以 *Test 後綴命名。例如:

kotlin
import kotlin.test.Test
import kotlin.test.assertEquals

class OrderServiceTest {
    @Test
    fun `calculate total should sum item prices`() {
        val service = OrderService()
        val result = service.calculateTotal(listOf(10.0, 25.0))
        assertEquals(35.0, result)
    }
}

撰寫整合測試

整合測試驗證組件之間的互動,例如服務與資料庫。 依照慣例,整合測試以 *IT 後綴命名。例如:

kotlin
import kotlin.test.Test
import kotlin.test.assertNotNull

class UserRepositoryIT {
    @Test
    fun saveFindUser() {
        // 與資料庫或服務整合的範例
        val repository = UserRepository()
        repository.save(User("KotlinUser"))
        
        val user = repository.findByName("KotlinUser")
        assertNotNull(user)
    }
}

執行測試

在 Maven 專案中,測試執行通常分為兩個外掛程式:Surefire 和 Failsafe,以確保乾淨的建置生命週期。

使用 Surefire 外掛程式

Surefire 外掛程式處理單元測試。 它會執行所有遵循 *Test 命名模式的 Kotlin 和 Java 測試。

預設情況下,它在建置生命週期的 test 階段執行,如果測試失敗,會立即使建置失敗。

xml
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.5.5</version>
</plugin>

若要僅執行單元測試,請使用以下指令:

bash
mvn test

使用 Failsafe 外掛程式

Failsafe 外掛程式處理整合測試。 它會執行所有遵循 *IT 命名模式的 Kotlin 和 Java 測試。

與 Surefire 不同,如果測試在 integration-test 階段失敗,Failsafe 允許建置繼續進行,從而允許 post-integration-test 階段的任務(例如停止 Docker 容器)執行。 如果存在任何測試失敗,建置最終會在 verify 階段失敗。

xml
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.5.5</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>

若要執行單元測試和整合測試,請使用以下指令:

bash
mvn verify

探索其他測試架構

除了 JUnit 之外,您還可以使用其他流行的架構,讓 Kotlin 測試更具慣用性且更易讀:

程式庫說明
AssertJ具有可鏈式斷言的流暢斷言程式庫。
Mockito-KotlinMockito 的 Kotlin 包裝函式,提供幫助函式並更好地與 Kotlin 型別系統整合。
MockK原生 Kotlin 模擬程式庫,支援 Kotlin 特定功能,包括協同程式和擴充方法。
KotestKotlin 的斷言程式庫,提供多種斷言樣式和廣泛的匹配器支援。
StriktKotlin 的斷言程式庫,具有型別安全斷言並支援資料類別。

後續步驟