Kotlin:使内部功能对于单元测试可见 [英] Kotlin: Make an internal function visible for unit tests

查看:240
本文介绍了Kotlin:使内部功能对于单元测试可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果测试位于与生产代码不同的模块中(这是常见的),那么使内部功能对于测试可见的最佳方法是什么?

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?

在Java中,我会将生产代码和测试放在同一个程序包中,并将要测试的方法设置为私有(加上,如果唯一原因将其打包,请添加@VisibleForTest批注-测试,而不是私有).不幸的是,Kotlin没有package-private的概念.

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.

推荐答案

标记为internal访问修饰符的类和方法将在当前版本的Kotlin,Gradle和Intellij中运行,以便从测试类访问这些方法.这些工具将主路径和测试源路径视为同一模块的一部分.

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.

这篇关于Kotlin:使内部功能对于单元测试可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆