How to make Kotlin `internal` objects accessible to tests?

纵然是瞬间 提交于 2020-02-25 02:08:13

问题


My project uses several Gradle source sets for its production code base instead of just main:

  • domain
  • dal
  • rest
  • test
  • dbUnitTest

This has proven very useful for limiting dependencies and enforcing separation of concern.

It comes with one downside however: we cannot access classes or methods with visibility internal from within test classes. The reason for this is that the Kotlin compiler places every source set in its own "module":

$ find . -name '*.kotlin_module'
./classes/kotlin/domain/META-INF/contact-management_domain.kotlin_module
./classes/kotlin/dal/META-INF/contact-management_dal.kotlin_module
./classes/kotlin/rest/META-INF/contact-management_dal.kotlin_module
./classes/kotlin/test/META-INF/contact-management.kotlin_module
./classes/kotlin/dbUnitTest/META-INF/contact-management_dbUnitTest.kotlin_module

I would like all sourceset to use the same module name "contact-management", as the main sourceset would by default.

I tried to override the name with the compiler option -module-name:

tasks.withType<KotlinCompile> {
    kotlinOptions {
        // place all source sets in the same kotlin module, making objects with 'internal' visibility available to every source sets of this project
        freeCompilerArgs += listOf("-module-name \"contact-management\")
    }
}

Upon running gradlew build, I get

> Task :contact-management:compileDomainKotlin FAILED
e: Invalid argument: -module-name "contact-management"

The reason being that -module-name "contact-management_domain" is set before by the Gradle code invoking the Kotlin compiler as well, but apparently this option is only accepted once.

In a Gradle build, how can I control what is being considered "one module" by the Kotlin compiler?

A related question where the test source set is to be split has no satisfactory answers so far.

来源:https://stackoverflow.com/questions/60346360/how-to-make-kotlin-internal-objects-accessible-to-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!