如何使用VisibleForTesting进行纯JUnit测试 [英] How to use VisibleForTesting for pure JUnit tests
问题描述
我正在对项目上的纯Java文件运行纯JUnit4 Java测试,但是我找不到使用
I´m running pure JUnit4 java tests over my pure java files on my project but I can't find a way to use @VisibleForTesting clearly without making the thing manually public.
例如:
@VisibleForTesting
public Address getAddress() {
return mAddress;
}
该方法必须为public
才能使其对测试公开",但是在那种情况下,注释没有意义吗?如果批注什么都不做,为什么不只使用注释呢?
The method has to be public
to let it be "public" to tests, but in that case the annotation doesn't make sense right? why not just use a comment if the annotation will not do nothing?
推荐答案
根据Android文档:
According to Android docs:
您可以选择指定可见性,如果不进行测试,应该是什么?这样,工具就可以从生产代码中捕获意外访问.
You can optionally specify what the visibility should have been if not for testing; this allows tools to catch unintended access from within production code.
示例:
@VisibleForTesting(否则= VisibleForTesting.PRIVATE) 公共地址getAddress()
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) public Address getAddress()
这篇关于如何使用VisibleForTesting进行纯JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!