testing - 无法从 Gradle 中的替代测试源集访问 Kotlin 内部成员

标签 testing gradle kotlin integration-testing visibility

正在关注 https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_testshttps://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:

  1. an IntelliJ IDEA module;
  2. a Maven project;
  3. a Gradle source set (with the exception that the test source set can access the internal declarations of main);
  4. 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/

相关文章:

安卓最高自定义通知的大小

ruby-on-rails - 使用多个数据库测试 Rails 应用程序

spring - Spring JDBC 测试的事务回滚

java - Flutter错误:无法下载bundletool.jar(com.android.tools.build:bundletool:0.9.0)

gradle - 在使用gradlefx与flexunit进行单元测试期间,如何加载RSL库?

gradle - flutter构建找不到依赖关系,但是当我将链接复制到浏览器时,它位于同一位置

java - 如何在 Spring Boot 应用程序上启用 Bearer 身份验证?

unit-testing - 我是否应该为每个类创建一个接口(interface)以使我的代码可测试(单元测试)

android - 应用内结算和许可证测试帐户

android - 升级到 RN 0.68 后,APK 中缺少 index.android.bundle