12

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.

Kousalik
  • 3,111
  • 3
  • 24
  • 46
  • This [related question](https://stackoverflow.com/q/60346360/484293) discusses some failed attempts. Did you happen to find a solution to your problem? – blubb Feb 21 '20 at 21:25

1 Answers1

2

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

gaiazov
  • 1,908
  • 14
  • 26
  • 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 '19 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, https://github.com/JetBrains/kotlin/blob/ece7a3e70e7c04cb2f599512284ed840e00936c6/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/metadata/KotlinMetadataTargetConfigurator.kt#L51-L57 – gaiazov Dec 10 '19 at 15:26
  • This answer talks about making IDEA happy: https://stackoverflow.com/a/35690003/2740621 – AutonomousApps Dec 26 '19 at 18:59