Skip to content

单元测试配置

JUnit 5 配置

kotlin
dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter:5.10.1")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
    
    // Mockito
    testImplementation("org.mockito:mockito-core:5.8.0")
    testImplementation("org.mockito:mockito-junit-jupiter:5.8.0")
    
    // AssertJ(流式断言)
    testImplementation("org.assertj:assertj-core:3.25.1")
}

tasks.named<Test>("test") {
    useJUnitPlatform()  // 必须声明使用 JUnit Platform
}

Spring Boot 测试

kotlin
dependencies {
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        // spring-boot-starter-test 已包含 JUnit 5、Mockito、AssertJ
    }
    testImplementation("com.h2database:h2")  // 内存数据库
    testImplementation("org.testcontainers:junit-jupiter:1.19.3")
    testImplementation("org.testcontainers:mysql:1.19.3")
}

tasks.named<Test>("test") {
    useJUnitPlatform()
}

测试过滤

bash
# 运行特定测试类
./gradlew test --tests "com.example.UserServiceTest"

# 运行特定方法
./gradlew test --tests "com.example.UserServiceTest.testCreate*"

# 使用通配符
./gradlew test --tests "com.example.*Test"

# 排除某些测试
./gradlew test --tests "!com.example.*SlowTest"
kotlin
// build.gradle.kts 中配置过滤
tasks.named<Test>("test") {
    useJUnitPlatform()
    
    filter {
        // 排除集成测试(带 @Tag("integration") 的测试)
        excludeTestsMatching("*IntegrationTest")
    }
}

JUnit 5 Tag 过滤

kotlin
tasks.named<Test>("test") {
    useJUnitPlatform {
        includeTags("fast", "unit")
        excludeTags("slow", "integration")
    }
}

tasks.register<Test>("integrationTest") {
    useJUnitPlatform {
        includeTags("integration")
    }
    shouldRunAfter(tasks.named("test"))
}

测试并行执行

kotlin
tasks.named<Test>("test") {
    useJUnitPlatform()
    
    // JVM 级别并行(每个子项目在独立 JVM 中)
    maxParallelForks = Runtime.getRuntime().availableProcessors()
    
    // JUnit 5 线程级并行(需要 junit-platform.properties 配置)
    systemProperty("junit.jupiter.execution.parallel.enabled", "true")
    systemProperty("junit.jupiter.execution.parallel.mode.default", "concurrent")
    systemProperty("junit.jupiter.execution.parallel.config.strategy", "fixed")
    systemProperty("junit.jupiter.execution.parallel.config.fixed.parallelism", "4")
}

测试 JVM 配置

kotlin
tasks.named<Test>("test") {
    useJUnitPlatform()
    
    // 内存配置
    minHeapSize = "256m"
    maxHeapSize = "1g"
    
    // JVM 参数
    jvmArgs("-XX:+UseG1GC", "-Dfile.encoding=UTF-8")
    
    // 系统属性(用于测试配置)
    systemProperty("spring.profiles.active", "test")
    systemProperty("testcontainers.reuse.enable", "true")
    
    // 环境变量
    environment("TEST_API_URL", "http://localhost:8080")
    
    // 测试超时
    timeout.set(java.time.Duration.ofMinutes(10))
}

测试日志

kotlin
tasks.named<Test>("test") {
    useJUnitPlatform()
    
    testLogging {
        events("passed", "skipped", "failed", "standardError")
        showExceptions = true
        showCauses = true
        showStackTraces = true
        exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
        showStandardStreams = false   // true 会输出所有 System.out
    }
}

下一步