How to suppress deprecation warnings when testing

2019-04-11 05:20发布

问题:

Suppose I have a library, which contains both a deprecated function and a preferred function:

object MyLib {
  def preferredFunction() = ()
  @deprecated("Use preferredFunction instead", "1.0") def deprecatedFunction() = ()
}

I want to test both preferredFunction and deprecatedFunction in ScalaTest:

class MyLibSpec extends FreeSpec with Matchers {
  "preferred function" in {
    MyLib.preferredFunction() should be(())
  }
  "deprecated function" in {
    MyLib.deprecatedFunction() should be(())
  }
}

However, a deprecation warning is reported at MyLib.deprecatedFunction().

How to avoid the warning?

回答1:

Scala does not support that, see https://groups.google.com/forum/#!topic/scala-internals/LsycMcEkXiA

However there is a plugin mentioned:

https://github.com/ghik/silencer

I haven't used it - so I am not sure if this works for your case.



回答2:

Just deprecate the class, which is instantiated reflectively by the test rig.

scala> @deprecated("","") def f() = ()
f: ()Unit

scala> @deprecated("","") class C { f() }
defined class C

scala> f()
<console>:13: warning: method f is deprecated:
       f()
       ^