Kotlin内部成员无法从Gradle中设置的替代测试源访问 [英] Kotlin internal members not accessible from alternative test source set in Gradle

查看:117
本文介绍了Kotlin内部成员无法从Gradle中设置的替代测试源访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循 https://docs.gradle.org/current/userguide /java_testing.html#sec:configuring_java_integration_tests

Following https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests and https://www.michael-bull.com/blog/2016/06/04/separating-integration-and-unit-tests-with-gradle we are attempting to separate our integration tests from plain unit tests.

我们遇到的问题是,此类测试无法访问Kotlin中的internal成员.根据 Kotlin doco ,测试源集存在可见性例外.

The problem we have is internal members in Kotlin are not accessible from such tests. As per Kotlin doco there is a visibility exception for test source sets.

内部可见性修饰符表示该成员可见 在同一模块中.更具体地说,一个模块是一组Kotlin 文件一起编译:

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. 一个IntelliJ IDEA模块;
  2. 一个Maven项目;
  3. Gradle源集(测试源集可以访问main的内部声明除外);
  4. 通过一次Ant任务的调用编译的一组文件.
  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.

除了不尝试访问它们以外,还有其他方法吗?那将需要对数百个测试进行重大重构,并有可能对整个代码库进行重构.

Is there a way around it other than not trying to access them? That would call for a major refactoring of hundreds of tests and potentialy the whole codebase.

推荐答案

通过将以下代码添加到我的自定义Gradle插件中,我能够获得自定义测试sourceSet来访问internal类.

I was able to get a custom test sourceSet to access internal classes by adding the following code to my custom Gradle plugin.

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源代码,发现了以下内容:

I looked at the kotlin-gradle-plugin source code and found the following: 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开心

With change, the tests in my custom source set run just fine, but IntellIJ still shows compilation errors. I'll look further to see if I can make IntelliJ happy as well

这篇关于Kotlin内部成员无法从Gradle中设置的替代测试源访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆