Gradle多测试套件共用同一源码集时执行integrationTest出现内部类访问编译错误
Gradle多测试套件共用同一源码集时执行integrationTest出现内部类访问编译错误
看起来你遇到的问题是Kotlin模块隔离导致的——自定义的integrationTest套件把测试代码当成了独立模块,所以没法访问主代码里的internal类,而默认的test任务因为和主代码同属一个模块,所以能正常运行。我帮你分析下原因并给出修复方案:
问题根源
- 你在
integrationTest套件里添加了implementation(project)依赖,这会把项目打包后的Jar作为外部依赖引入,导致测试代码和主代码被视为两个独立模块。而Kotlin的internal修饰符仅允许同一模块内的代码访问,自然就会出现编译错误。 - 你手动拼接的编译类路径没有正确继承
test源码集的环境,缺少了主代码的编译输出关联,进一步加剧了模块隔离的问题。
修复后的配置代码
@Suppress("UnstableApiUsage") testing { suites { configureEach { if (this is JvmTestSuite) { useJUnitJupiter() dependencies { implementation("org.springframework.boot:spring-boot-starter-test") implementation("io.projectreactor:reactor-test") implementation("org.springframework.security:spring-security-test") implementation("io.mockk:mockk:1.13.5") implementation("com.ninja-squad:springmockk:4.0.0") implementation("org.testcontainers:junit-jupiter") implementation("org.testcontainers:r2dbc") implementation("org.testcontainers:postgresql") } targets { all { testTask.configure { /* 保持原有的JVM参数配置 */ jvmArgs = listOf("--add-opens=java.base/java.lang.reflect=ALL-UNNAMED") } } } } } val test by getting(JvmTestSuite::class) { targets { all { testTask.configure { minHeapSize = "512m" maxHeapSize = "2048m" finalizedBy(tasks.jacocoTestReport) filter { excludeTestsMatching("*Integration*Test") excludeTestsMatching("*.integration.*") excludeTestsMatching("*Repository*Test") excludeTestsMatching("*Controller*Test") } } } } } register<JvmTestSuite>("integrationTest") { useJUnitJupiter() // 复用test源码集的所有源码和资源目录 sources { java.setSrcDirs(test.sources.java.srcDirs) kotlin.setSrcDirs(test.sources.kotlin.srcDirs) resources.setSrcDirs(test.sources.resources.srcDirs) // 直接复用test源码集的编译/运行类路径,确保和test任务环境一致 compileClasspath = project.sourceSets.test.compileClasspath runtimeClasspath = project.sourceSets.test.runtimeClasspath } targets { all { testTask.configure { shouldRunAfter(test) filter { includeTestsMatching("*IntegrationTest") } } } } } } }
关键修改点说明
- 移除
implementation(project)依赖:避免将项目Jar作为外部依赖,让测试代码和主代码保持在同一模块内,从而可以正常访问internal类。 - 复用
test源码集的类路径:test的类路径已经包含了主代码的编译输出、所有测试依赖,直接复用可以保证integrationTest的编译/运行环境和test任务完全一致,不需要手动拼接。 - 保持源码目录复用:继续指向
test的源码目录,满足你“单源码集多测试套件”的需求。
这个修改应该能解决你遇到的内部类访问编译错误,同时保留你原本的测试过滤逻辑。
备注:内容来源于stack exchange,提问作者Stuck
相关产品推荐
相关产品推荐

