2

What string should I use in @SuppressWarnings("....") to remove the warning I get in this unit test of a method with varargs:

/**
 * Calling {@link #readFiles(String, String...)} with just one arg and a null
 * works, but you have to do a null check on the varargs in case it is null.
 */
@Test
public void callMethodWithOneArgAndNull() {
   readFiles("C:/Temp", null);
}

Eclipse gives the following warning for this:

The argument of type null should explicitly be cast to String[] for the invocation of the varargs method readFiles(String, String...) from type TestVarArgs. It could alternatively be cast to String for a varargs invocation

This is good; it is expected. I am also unit testing what happens when I cast the null.

But for the purposes of this single test, I want to suppress that specific warning, i.e. I don't want to use @SuppressWarnings("all"). I have tried every string mentioned in this answer to What is the list of valid @SuppressWarnings warning names in Java? and none of them seem to catch this warning.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Robert Mark Bram
  • 8,104
  • 8
  • 52
  • 73

2 Answers2

3

To my understanding, this is not a warning you can suppress with a specific annotation. You have to make an explicit cast to make the warning go away - or suppress all warnings.

Also, consider testing both possible casts, i.e. (String[]) {null} and (String) null, to make sure your method doesn't choke on "no files" nor on a "null filename" input.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Good pointers @einpoklum, thanks. I wrote up these tests here: http://robertmarkbramprogrammer.blogspot.com.au/2013/03/nulls-and-varargs.html – Robert Mark Bram Mar 31 '13 at 11:54
-1

You can use @supresswarnings (unchecked) annotation.

Protagonist
  • 1,649
  • 7
  • 34
  • 56