[英]IntelliJ: get a warning if I call a method annotated with @VisibleForTesting
I have the following use case: 我有以下用例:
I want to Unit-test a private method (I really want to). 我想对一个私有方法进行单元测试(我真的很想)。 Now, there are different approaches, but I want to choose the approach "make the method package private".
现在,有不同的方法,但是我想选择“使方法包私有”的方法。
In Guava, there's an annotation called @VisibleForTesting . 在番石榴中,有一个名为@VisibleForTesting的注释。 What I want to do is: Whenever this method is called from another class (in the same package, of course), regardless of it's a subclass of this class or not, display a warning like "Hey dude, the method you want to call is marked with @VisibleForTesting, are you sure you want to call it?".
我想做的是:每当从另一个类 (当然,在同一个包中)调用此方法时,无论它是否是此类的子类,均显示警告,例如“嘿,伙计,您要调用的方法标有@VisibleForTesting,您确定要调用它吗?”。
I played a bit with custom inspections , but with no luck so far. 我玩了一些自定义检查 ,但是到目前为止还没有运气。 Does anyone have an idea?
有人有主意吗?
There is no need for a custom inspection, the necessary inspection is built in. Go to Preferences | Editor | Inspections
没有必要为一个自定义的检查,必要的检查是内置的进入。
Preferences | Editor | Inspections
Preferences | Editor | Inspections
Preferences | Editor | Inspections
and enable the inspection Java | General | Test-only class or method call in production code
Preferences | Editor | Inspections
并启用检查Java | General | Test-only class or method call in production code
Java | General | Test-only class or method call in production code
Java | General | Test-only class or method call in production code
. Java | General | Test-only class or method call in production code
。 Now you will get a warning if you try to call members with the @VisibleForTesting
annotation from production code, but not from test code. 现在,如果您尝试从生产代码而不是从测试代码调用带有
@VisibleForTesting
批注的成员,则会收到警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.