How to suppress deprecation warnings when testing deprecated Scala functions?

元气小坏坏 提交于 2019-12-06 06:38:13

问题


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()
       ^


来源:https://stackoverflow.com/questions/52837691/how-to-suppress-deprecation-warnings-when-testing-deprecated-scala-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!