I'm mainly a Java developer and wonder about structure when writing unit test in kotlin,
Assuming there's no package-private in kotlin
private
to restrict visibility to the file
internal
to restrict visibility to the module
How can I open class only to test class ?
Must I write test inside kotlin class or open class to all module (internal)?
What's the kotlin way to open method for unit test only?
EDIT
Found similar question/request in kotlin discuss by @bentolor:
How am I supposed to do unit / whitebox testing properly? I want to write test code which tests class-internal functionality which I do not want to expose to other classes except my test class at all.
The package protected visibility is an excellent way to achieve this. Whereas Kotlin now requires me to make these methods effectively public and litter the visible API of my component all-over the project be able to test them.
In my view internal is more or less public as it has a much larger scope. Most projects have sth. around 1 - 5 “modules” in the Kotlin sense.
Really strongly asking/advocating for package-local visibility here.
open
your class for testing only (e.g. to stub it in unit tests for other classes) – which I guess is your question, and I'd be interested in a possible solution, too – however if you want to unit test an internal method you should do it by calling public methods of that class; I don't think it's correct to "open" a method that's meant to be private just so you can unit test it – Tatouayinternal
really is what you should use for this purpose. – Timbre