IntelliJ:如果我调用用@VisibleForTesting 注释的方法,则会收到警告

IntelliJ: get a warning if I call a method annotated with @VisibleForTesting

我有以下用例:

我想对私有方法进行单元测试(我真的很想)。 现在,有不同的方法,但我想选择方法 "make the method package private"。

在 Guava 中,有一个名为 @VisibleForTesting 的注解。我想做的是: 每当从另一个 class(当然是在同一个包中)调用此方法时 ,无论它是此 class 的子 class 还是不,显示警告,例如 "Hey dude, the method you want to call is marked with @VisibleForTesting, are you sure you want to call it?"。

我玩过 custom inspections,但到目前为止运气不好。有人有想法吗?

不需要自定义检查,必要的检查是内置的。转到 Preferences | Editor | Inspections 并启用检查 Java | General | Test-only class or method call in production code。现在,如果您尝试从生产代码而非测试代码中使用 @VisibleForTesting 注释调用成员,您将收到警告。