Reputation: 56572
I'm trying to suppress a FindBugs warning in one of my unit tests:
@SuppressFBWarnings(value = "DMI_DOH",
justification = "It's sensical when declaring expectations.")
@Test
public void ctor_checksForNullArguments() {
new Expectations(Preconditions.class) {
{
Preconditions.checkNotNull("DUMMY"); // D'oh! A nonsensical invocation…
this.result = "DUMMY";
Preconditions.checkNotNull("(dummy)"); // D'oh! A nonsensical invocation…
this.result = "(dummy)";
}
};
new InfixSymbolTest.DummySymbol();
}
I can only assume that I'm doing so incorrectly, as the warning is still reported via both the Eclipse feature and Gradle plugin. Help?
Upvotes: 2
Views: 1054
Reputation: 4102
The following code creates an anonymous inner class:
new Expectations(Preconditions.class) {
// ...
};
The calls to Preconditions.checkNotNull
and assignments to this.result
occur within an initializer block scoped within the anonymous inner class. In order for the @SuppressFBWarnings
annotation to apply to those statements, the annotation must be placed, at minimum, on the class that contains them.
Though the source code for the anonymous inner class and its statements are physically written within the ctor_checksForNullArguments
test method, the resulting bytecode is not actually a part of the test class, nor that method, and so placing the annotation on the method applies it to the wrong scope.
Since any anonymous inner class can be extracted to either a named (non-anonymous) inner class or even a top-level class, doing so will enable you to place @SuppressFBAnnotations
on the proper scope: either the entire class, or potentially a more restricted scope such as a particular method or constructor (as needed).
Upvotes: 2