13

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.

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.

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.

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.

1
  • This related question discusses some failed attempts. Did you happen to find a solution to your problem?
    – blubb
    Feb 21, 2020 at 21:25

1 Answer 1

4

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));

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

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

3
  • That's a useful pointer but I cannot manage to translate this into anything in the Groovy DSL (though I am trying with MPP in the mix too): kotlin { ... targets.jvm.compilations.test.associateWith targets.jvm.compilations.main }. Seems associateWith is not exposed for users.
    – drekbour
    Dec 10, 2019 at 14:36
  • In MPP I think you need to add a dependency on compileMain kotlin source set. There are several references to the compileMain and compileTest sources sets. For example, github.com/JetBrains/kotlin/blob/…
    – gaiazov
    Dec 10, 2019 at 15:26
  • This answer talks about making IDEA happy: stackoverflow.com/a/35690003/2740621 Dec 26, 2019 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.