如果测试位于与生产代码不同的模块中(这是常见的),那么使内部函数对测试可见的最佳方式是什么?

In Java, I would have the production code and the test in the same package and make the methods-to-be-tested package-private (plus, add a @VisibleForTest annotation if the only reason for having it package-private rather than private is the test). Unfortunately, Kotlin doesn't have the concept of package-private.

推荐答案

Classes and methods marked with internal access modifier will work from within current versions of Kotlin, Gradle and also Intellij for accessing those methods from test classes. The tools consider the main and test source paths as part of the same module.

你已经试过了吗?如果失败了,你应该报告一个bug,因为这个bug已经被报告、修复,并且在任何当前版本中都可以.

Kotlin相关问答推荐

Kotlin-删除按钮周围的空格

在Mapstruct中重用@映射定义

如何在 Kotlin 中初始化 Short 数组?

Kotlin - 如何避免在密封类的 when() 语句中转换第二个变量

TzdbZoneRulesProvider 在 java.time 中不工作

Kotlin 数据类中的大量参数

你怎么知道什么时候需要 yield()?

如何使用 Android CameraX 自动对焦

如何从 Java 中隐藏 Kotlin 的 lateinit var 支持字段?

Kotlinwhen表达式在使用主题时是否支持复合布尔表达式?

Kotlin 中的数据类

如何在顶级函数中使用 koin 注入依赖项

使用 clear() 删除 EncryptedSharedPreferences 不起作用

Android Studio 和 Kotlin:Unresolved reference: also

如何在Kotlin中创建无限长的序列

在Kotlin中使用@Service时引发异常

使用导航组件在不同的图形之间导航

安装 Kotlin-Jupyter:e: java.lang.NoClassDefFoundError: 无法初始化类 org.jetbrains.kotlin.com.intellij.pom.java.LanguageLevel

Android Kotlin .visibility

在 IntelliJ Idea 中未为 Kotlin @ConfigurationProperties 类生成 spring-configuration-metadata.json 文件