7

When testing public constructor calls the JUnit tests in my application produce temporary objects that are never used anywhere within the test methods. The compiler subsequently complains about unused object allocation. Is there a way to suppress compiler warnings selectively for all JUnit tests? The tests are in a separate package.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Petr
  • 1,128
  • 2
  • 14
  • 23
  • 1
    Check this : http://stackoverflow.com/questions/593996/how-to-suppress-java-compiler-warnings-for-specific-functions – user47900 Jan 24 '12 at 22:02
  • @user47900 Thanks, but I was hoping for a more global approach (package-level supression). Although, this solution is probably safer and generally recommended. – Petr Jan 24 '12 at 22:06
  • javac -nowarn. I am not sure if I there is an option for specific package. – user47900 Jan 24 '12 at 22:12

1 Answers1

7

I think the answer is no, not at the package level. I tend to "cheat" and define my inner objects for testing purposes as protected. That works around the "unused" warnings at least:

protected static class TestFoo {
   ...
}

As @user47900 pointed out, you can obviously use the SuppressWarnings annotation to get around a single warning but they usually need to be defined per class and cannot cover all inner classes nor packages.

@SuppressWarnings("unused")
private static class TestFoo {
   ...
}
Gray
  • 115,027
  • 24
  • 293
  • 354