正在关注 https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests和 https://www.michael-bull.com/blog/2016/06/04/separating-integration-and-unit-tests-with-gradle我们正试图将我们的集成测试与普通单元测试分开。
我们遇到的问题是 Kotlin 中的 internal
成员无法从此类测试中访问。根据 Kotlin doco测试源集存在可见性异常。
The internal visibility modifier means that the member is visible within the same module. More specifically, a module is a set of Kotlin files compiled together:
- an IntelliJ IDEA module;
- a Maven project;
- a Gradle source set (with the exception that the test source set can access the internal declarations of main);
- a set of files compiled with one invocation of the Ant task.
除了不尝试访问它们之外,还有其他解决方法吗?这将需要对数百个测试和潜在的整个代码库进行重大重构。
最佳答案
通过将以下代码添加到我的自定义 Gradle 插件,我能够获得自定义测试源集来访问 internal
类。
NamedDomainObjectContainer<KotlinWithJavaCompilation<KotlinJvmOptions>> compilations = project
.getExtensions()
.getByType(KotlinJvmProjectExtension.class)
.target.getCompilations();
compilations.getByName(sourceSet.getName())
.associateWith(compilations.getByName(SourceSet.MAIN_SOURCE_SET_NAME));
查看了kotlin-gradle-plugin
源码,发现如下:
https://github.com/JetBrains/kotlin/blob/v1.3.61/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPlugin.kt#L488-L490
随着更改,我的自定义源集中的测试运行得很好,但 IntellIJ 仍然显示编译错误。我会进一步看看我是否也能让 IntelliJ 开心
关于testing - 无法从 Gradle 中的替代测试源集访问 Kotlin 内部成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57050889/