Kotlin: Make an internal function visible for unit tests

自闭症网瘾萝莉.ら 提交于 2019-12-19 12:24:25

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/35683991/kotlin-make-an-internal-function-visible-for-unit-tests

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