Java 如何使用 VisibleForTesting 进行纯 JUnit 测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33267820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use VisibleForTesting for pure JUnit tests
提问by Daniel Gomez Rico
I′m running pure JUnit4 java tests over my pure java files on my project but I can't find a way to use @VisibleForTestingclearly without making the thing manually public.
我正在对我的项目中的纯 Java 文件运行纯 JUnit4 java 测试,但我找不到一种方法来清楚地使用@VisibleForTesting而不手动公开该内容。
Ex:
前任:
@VisibleForTesting
public Address getAddress() {
return mAddress;
}
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?
该方法必须public
让它对测试“公开”,但在这种情况下,注释没有意义,对吗?如果注释什么都不做,为什么不直接使用注释?
采纳答案by Daniel Gomez Rico
The Tag itself helps with the linter to identify unwanted access.
标签本身有助于 linter 识别不需要的访问。
To lower the risk of use it directly, add this methods as internal
in Kotlinor protected
in Javainstead of public
and with that only the tests or classes that are in the same package will be able to access that method.
为了降低使用风险直接,添加此方法是internal
在科特林或protected
在Java的替代public
,并与仅是在同一个包就能访问该方法测试或类。
Java:
爪哇:
@VisibleForTesting
protected Address address() {
return mAddress;
}
Kotlin:
科特林:
@VisibleForTesting
internal fun address(): Address {
return address;
}
回答by MikeJ
Make the method package-private and the test will be able to see it, if the test is in the corresponding test package (same package name as the production code).
如果测试在相应的测试包中(与生产代码相同的包名),则将方法设置为 package-private 并且测试将能够看到它。
@VisibleForTesting
Address getAddress() {
return mAddress;
}
Also consider refactoring your code so you don't need to explicitly test a private method, try testing the behaviour of a public interface. Code that is hard to test can be an indication that improvements can be made to production code.
还要考虑重构你的代码,这样你就不需要显式测试私有方法,尝试测试公共接口的行为。难以测试的代码可能表明可以对生产代码进行改进。
The point of an annotation is that its convention and could be used in static code analysis, whereas a comment could not.
注释的重点是它的约定,可以用于静态代码分析,而注释则不能。
回答by Danail Tsvetanov
@VisibleForTesting annotation is used in package-methods in Guava, and does not part of JUnit API. The annotation is just a tag to indicate the method can be tested. It even doesn't be loaded in JVM.
@VisibleForTesting 注解用在 Guava 的 package-methods 中,不属于 JUnit API。注释只是一个标记,表明该方法可以被测试。它甚至不会加载到 JVM 中。
回答by Rodrigo Borba
According to Android docs:
根据 Android 文档:
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.
如果不用于测试,您可以选择指定可见性应该是什么;这允许工具从生产代码中捕获意外访问。
Example:
例子:
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) public Address getAddress()
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) 公共地址 getAddress()