测试已弃用的Scala函数时,如何抑制弃用警告?

时间:2018-10-16 14:22:18

标签: scala unit-testing testing deprecated scalatest

假设我有一个库,其中包含不推荐使用的函数和首选函数:

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

我想在ScalaTest中同时测试preferredFunctiondeprecatedFunction

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

但是,在MyLib.deprecatedFunction()上报告了弃用警告。

如何避免警告?

2 个答案:

答案 0 :(得分:2)

Scala不支持,请参见https://groups.google.com/forum/#!topic/scala-internals/LsycMcEkXiA

但是提到了一个插件:

https://github.com/ghik/silencer

我还没有使用过-所以我不确定这是否适合您的情况。

答案 1 :(得分:1)

只需弃用该类,该类将由测试装备以反射方式实例化。

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

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

scala> f()
<console>:13: warning: method f is deprecated:
       f()
       ^
相关问题