Make an internal function visible for unit tests
Asked Answered
I

3

38

In case the tests are in a different module than the production code (which is common), what's the best way to make internal functions visible for tests?

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.

Irredentist answered 28/2, 2016 at 14:52 Comment(1)
Maybe theinternal modifier can help you here: kotlinlang.org/docs/reference/visibility-modifiers.htmlAnalemma
S
30

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.

Did you try this already? And if it failed you should report a bug since this was already reported, fixed and should be fine in any current version.

Septarium answered 28/2, 2016 at 14:52 Comment(8)
Yes, that's exactly what I have tried with Intellij IDEA 15. I'll double-check I'm at the latest version of both the IDE and the Kotlin plug-in and that my setup is correct.Shutz
Latest Intellij IDEA 15 is 15.0.4, with Kotlin plugin 1.0.0Septarium
It's working correctly now and I haven't even installed any updates. I have probably done something wrong before and got confused. Cool that it's working! Thanks.Shutz
Unless I'm doing something similarly wrong it doesn't seem to work from non-Kotlin unit tests, though (Spock + Groovy in my case) :(Tablespoon
> The tools consider the main and test source paths as part of the same module. Where does this magic happen? I am trying to use Buck to build and run my Kotlin tests.Tichon
@Tichon Please ask that as a new question, Buck likely is not setting them up the compilation module in the same way that Intellij, Eclipse, Maven, and Gradle do to make this work. And Buck is a unique creature.Septarium
@GregorPetrin The Spock issue doesn't seem to be solved yet: youtrack.jetbrains.com/issue/KT-525Augment
This doesn't seem to be true as of Kotlin 1.8.20...Loanloanda
C
7

Probably the easiest solution is to place your unit tests depending on internal code in the same module with the production сode and leave only the integration tests, which use public API, in the separate module.

This seems reasonable since the internal modifier means exactly the visibility inside the same module.

Cytogenetics answered 28/2, 2016 at 18:7 Comment(0)
A
1

In my case it didn't work because we renamed our projects inside settings.gradle to add a prefix and Kotlin didn't seem to like that.

Once we removed the lines renaming the projects, we were able to see internal members from main/ inside test/.

Ardelia answered 5/4, 2022 at 5:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.