使用 Maven 测试 Kotlin 项目
Kotlin 与 Maven 生态系统无缝集成,允许您使用行业标准工具验证后端应用程序。在本指南中,您将学习如何使用 JUnit 创建测试,并使用 Maven 插件运行单元测试和集成测试。
使用 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 文件:
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>2.4.10</version>
<scope>test</scope>
</dependency>
</dependencies>尽管其名称如此,
kotlin-test-junit5仍支持所有最新的 JUnit 版本,包括 JUnit 6。
JUnit 4
如果您想使用较早版本的 JUnit(例如用于旧版项目),请使用利用 JUnit 4 的 kotlin-test-junit 构件:
<!-- pom.xml -->
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit</artifactId>
<version>2.4.10</version>
<scope>test</scope>
</dependency>
</dependencies>有关使用 JUnit 进行测试的详细指南和示例项目,请参阅使用 Kotlin 测试 Java 代码教程。
编写单元测试
单元测试验证代码的隔离部分,例如单个函数或类。 按照约定,单元测试以 *Test 后缀命名。例如:
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 后缀命名。例如:
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 阶段执行,如果测试失败,则会立即导致构建失败。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.5</version>
</plugin>要仅运行单元测试,请使用以下命令:
mvn test使用 Failsafe 插件
Failsafe 插件处理 集成测试。 它运行所有遵循 *IT 命名模式的 Kotlin 和 Java 测试。
与 Surefire 不同,Failsafe 允许构建在 integration-test 阶段即使有测试失败也继续进行,从而允许 post-integration-test 阶段的任务(如停止 Docker 容器)运行。 如果有任何测试失败,构建最终会在 verify 阶段失败。
<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>要同时运行单元测试和集成测试,请使用以下命令:
mvn verify获取详细的失败消息
Kotlin Power-assert 编译器插件生成详细的失败消息,显示断言中的中间值,在控制台输出中为您提供完整的图表。
要启用 Power-assert,请将以下配置添加到项目的
pom.xml中:xml<properties> <kotlin.version>2.4.10</kotlin.version> <kotlin.compiler.jdkRelease>17</kotlin.compiler.jdkRelease> </properties> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-test-junit5</artifactId> <version>${kotlin.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>${kotlin.version}</version> <extensions>true</extensions> <executions> <execution> <id>compile</id> <phase>process-sources</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <phase>process-test-sources</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> <configuration> <!-- 指定 Power-assert 插件 --> <compilerPlugins> <plugin>power-assert</plugin> </compilerPlugins> <!-- 指定要转换的函数 --> <pluginOptions> <option>power-assert:function=kotlin.test.assertEquals</option> <option>power-assert:function=kotlin.test.assertTrue</option> </pluginOptions> </configuration> <!-- 添加 Power-assert 插件依赖项 --> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-power-assert</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> </plugin> </plugins> </build>- 在
<properties>部分,设置 Kotlin 版本和目标 JDK 发行版本,例如 JDK 17。 - 在
<dependencies>部分,添加kotlin-test-junit5用于运行测试。 - 在
<build><plugins>部分,使用power-assert编译器插件配置kotlin-maven-plugin。 在<pluginOptions>下列出您想要转换的断言函数,并添加 Power-assert 插件依赖项。
- 在
在
src/test/kotlin目录中创建一个测试文件UserProfileBackendTest.kt:kotlinimport kotlin.test.Test import kotlin.test.assertEquals class UserProfileBackendTest { data class UserProfile(val id: Long, val email: String) @Test fun `verify backend entity mapping matches`() { // 定义预期的对象状态 val expectedRecord = UserProfile(id = 451L, email = "admin-dev@company.internal") // 模拟不匹配的后端响应 val actualRecord = UserProfile(id = 451L, email = "admin-prod@company.com") // 使用标准的 kotlin.test 断言 assertEquals(expectedRecord, actualRecord, "Profile configurations out of sync") } }运行测试并验证输出:
bashmvn test
控制台会显示完整的 Power-assert 图表,显示表达式中每个位置的预期值与实际值之间的差异:
Profile configurations out of sync
assertEquals(expectedRecord, actualRecord, "Profile configurations out of sync")
| |
| UserProfile(id=451, email=admin-prod@company.com)
UserProfile(id=451, email=admin-dev@company.internal)探索其他测试框架
除 JUnit 外,您还可以使用其他流行框架使 Kotlin 测试更加惯用且易读:
| 库 | 描述 |
|---|---|
| AssertJ | 具有可链式调用断言的流式断言库。 |
| Mockito-Kotlin | Mockito 的 Kotlin 包装器,提供辅助函数并能更好地与 Kotlin 类型系统集成。 |
| MockK | 原生 Kotlin 模拟库,支持 Kotlin 特定功能,包括协程和扩展函数。 |
| Kotest | Kotlin 断言库,提供多种断言样式和广泛的匹配器支持。 |
| Strikt | Kotlin 断言库,具有类型安全断言并支持数据类。 |
后续步骤
- 探索
kotlin.test库的功能。 - 详细了解 Power-assert 编译器插件。
