第2章 测试环境搭建:Gradle/Maven配置kotlin.test依赖
说实话,很多团队在开始做Kotlin回归测试时翻的第一个跟头,就是依赖配错了。我记得有个同事花了一整天找bug,最后发现是testImplementation写成了implementation——这种低级错误浪费的时间太不值了。所以我习惯先把这章讲透,后面你写测试用例就不会被环境卡脖子。
2.1 依赖的核心思路
kotlin.test是Kotlin官方提供的测试库,它不像JUnit那样需要额外引入注解包,因为它本身就是Kotlin写的。说白了,你只需要在构建文件里加一行依赖声明,剩下的IDE会自动帮你搞定。
关键要点: kotlin.test默认依赖JUnit5的引擎(JUnit Jupiter),但你也可以换成JUnit4。我个人推荐用JUnit5,原因后面会讲。
2.2 Gradle配置(最常用)
现在大部分Kotlin项目都用Gradle,尤其是Android项目。咱们直接看示例:
// build.gradle.kts (Kotlin DSL)
plugins {
kotlin("jvm") version "1.9.0"
id("org.jetbrains.kotlin.plugin.test") version "1.9.0" // 可选的测试插件
}
dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-test:1.9.0")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5:1.9.0")
// 如果使用JUnit4,换成下面这行:
// testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.9.0")
}
tasks.withType<Test> {
useJUnitPlatform() // 告诉Gradle用JUnit5引擎
}
⚠️ 曾经遇到过的坑: 我曾经在一台CI机器上没加useJUnitPlatform(),结果所有测试都显示“No tests found”。折腾半天才发现是引擎没被激活。所以这句一定要写。
如果用Groovy DSL(老项目常见),写法稍微不同:
dependencies {
testImplementation 'org.jetbrains.kotlin:kotlin-test:1.9.0'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5:1.9.0'
}
test {
useJUnitPlatform()
}
2.3 Maven配置
有些后端团队还在用Maven,配置也不复杂:
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>1.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>1.9.0</version>
<scope>test</scope>
</dependency>
然后用maven-surefire-plugin指定引擎:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<useModulePath>false</useModulePath>
<includes>
<include>*Test.*</include>
</includes>
</configuration>
</plugin>
💡 小技巧: 记得把kotlin-test和kotlin-test-junit5的版本号保持一致。我见过有人写了不同版本,结果运行时类冲突。用BOM管理依赖会更省心。
2.4 版本选择与兼容性
你想想看,kotlin.test每个大版本会跟着Kotlin主版本走。比如Kotlin 1.8系列对应kotlin-test 1.8.x,1.9对应1.9.x。不建议混搭,除非你清楚自己在做什么。
下面这张表是我自己整理的常用版本对应关系:
| Kotlin版本 | kotlin-test最新小版本 | 对应JUnit版本 | 备注 |
|---|---|---|---|
| 1.6.x | 1.6.21 | JUnit5 5.8.x | 已停止更新 |
| 1.7.x | 1.7.22 | JUnit5 5.9.x | 建议升级 |
| 1.8.x | 1.8.22 | JUnit5 5.9.x | 稳定版本 |
| 1.9.x | 1.9.22 | JUnit5 5.10.x | 推荐使用 |
2.5 本地仓库验证
配置完之后,你可以手动跑个简单的测试来验证环境是否正常。我通常会用两步:
- 命令行运行:
./gradlew test或mvn test - 检查输出:看到“BUILD SUCCESSFUL”就说明依赖没断,引擎正常。
如果报错,八成是版本冲突。先看看有没有多个kotlin-test依赖混在一起,或者JUnit引擎冲突。你可以在构建日志中开启依赖分析:
// Gradle 查看依赖树
./gradlew dependencies --configuration testRuntimeClasspath
// Maven
mvn dependency:tree -Dscope=test
2.6 本章核心流程
下面这张SVG图帮你理清从创建项目到跑起来的所有步骤:
2.7 多模块项目中的依赖管理
项目大了肯定会分模块。这时候不要在每个模块里重复写版本号。我习惯在根项目的build.gradle.kts中用subprojects统一管理:
subprojects {
dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-test:1.9.0")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5:1.9.0")
}
tasks.withType<Test> {
useJUnitPlatform()
}
}
如果你用Maven,在父POM的<dependencyManagement>里声明版本,子模块引用时就不用写版本号了。嗯,这个小技巧能让版本升级时只改一处。
最后的提醒: 我个人强烈建议在.gitignore里加上.gradle和build/目录,但kotlin-test的本地缓存不需要提交。还有人喜欢用Gradle的Wrapper统一构建版本,这招在团队协作里特别管用。