我們需要使用 mock-maker-inline 來模擬一些第三方庫(例如 Azure SDK)的最終類。
我們使用以下版本的 scalatest 和 mockito:
scalaVersion := "2.12.2"
val ScalaTestVersion = "3.2.5"
val ScalaCheckVersion = "1.14.2"
val MockitoVersion = "3.4.0"
val DockerItVersion = "0.9.9"
val MockJavaMailVersion = "1.9"
val MockitoScalaVersion = "1.1.4"
val ScalaPlusScalaCheckVersion = "3.2.2.0"
val ScalaPlusMockitoVersion = "3.2.10.0"
lazy val MockitoIssueSample = (project in file("."))
.settings(
name := "MockitoIssueSample",
libraryDependencies = "org.scalatest" %% "scalatest" % ScalaTestVersion % Test,
libraryDependencies = "org.scalacheck" %% "scalacheck" % ScalaCheckVersion % Test,
libraryDependencies = "org.mockito" % "mockito-core" % MockitoVersion % Test,
libraryDependencies = "org.mockito" %% "mockito-scala" % MockitoScalaVersion % Test,
libraryDependencies = "org.scalatestplus" %% "scalacheck-1-14" % ScalaPlusScalaCheckVersion % Test,
libraryDependencies = "org.scalatestplus" %% "mockito-3-4" % ScalaPlusMockitoVersion % Test,
)
在我們的 Scala 應用程式中啟用 mock-maker-inline 后,其他使用具有 final 方法的 trait 的測驗用例開始失敗,并出現以下錯誤:
[info] - should should invoke area of the appropriate shape *** FAILED ***
[info] org.mockito.exceptions.misusing.UnnecessaryStubbingException: Unnecessary stubbings detected.
[info] Clean & maintainable test code requires zero unnecessary code.
[info] Following stubbings are unnecessary (click to navigate to relevant line of code):
[info] 1. -> at cortex.mockito.sample.AreaCalculatorSpec$$anon$1.<init>(AreaCalculatorSpec.scala:27)
[info] Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.
[info] at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
[info] at scala.Option.fold(Option.scala:158)
[info] at cortex.mockito.sample.AreaCalculatorSpec.withFixture(AreaCalculatorSpec.scala:13)
[info] at org.scalatest.wordspec.AnyWordSpecLike.invokeWithFixture$1(AnyWordSpecLike.scala:1075)
[info] at org.scalatest.wordspec.AnyWordSpecLike.$anonfun$runTest$1(AnyWordSpecLike.scala:1087)
[info] at org.scalatest.SuperEngine.runTestImpl(Engine.scala:306)
[info] at org.scalatest.wordspec.AnyWordSpecLike.runTest(AnyWordSpecLike.scala:1087)
[info] at org.scalatest.wordspec.AnyWordSpecLike.runTest$(AnyWordSpecLike.scala:1069)
[info] at org.scalatest.wordspec.AnyWordSpec.runTest(AnyWordSpec.scala:1879)
[info] at org.scalatest.wordspec.AnyWordSpecLike.$anonfun$runTests$1(AnyWordSpecLike.scala:1146)
[info] ...
我們已經用測驗 Scala 應用程式模擬了這個問題。如果我們禁用 mock-maker-inline 那么這個測驗用例可以作業。在這里,在這個示例應用程式中,我們只添加了一個有問題的測驗用例。
下面是示例代碼:
形狀.scala
包 mockito.sample
特征形狀{
final def printArea(): Unit = { println(s"Area is: $getArea()") }
def getArea(): 雙倍
}
矩形.scala
包 mockito.sample
類矩形(l:長,b:長)擴展形狀{
覆寫 def getArea(): Double = { l * b }
}
面積計算器.scala
包 mockito.sample
類面積計算器(形狀:形狀){
def printArea(): Boolean = { shape.printArea() true }
}
AreaCalculatorSpec.scala
包 mockito.sample
匯入 org.mockito.integrations.scalatest.IdiomaticMockitoFixture
匯入 org.scalatest.concurrent.ScalaFutures
匯入 org.scalatest.matchers.must.Matchers.convertToAnyMustWrapper
匯入 org.scalatest.matchers.should.Matchers
匯入 org.scalatest.wordspec.AnyWordSpec
匯入 org.scalatest.{EitherValues, TryValues}
匯入 org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
類 AreaCalculatorSpec 擴展 AnyWordSpec 與 Matchers 與 ScalaFutures 與 EitherValues 與 TryValues 與 IdiomaticMockitoFixture 與 ScalaCheckPropertyChecks {
特征設定 { val rectangle = mock[Rectangle] val areaCalculator = new AreaCalculator(rectangle) }
"AreaCalculator#printArea" 應該 { "應該呼叫適當形狀的區域" 在新設定 { rectangle.getArea() shouldReturn 40.0 areaCalculator.printArea() mustBe true } }
}
請檢查并建議您的寶貴意見。讓我知道是否需要任何其他詳細資訊。
謝謝,
拉克什·丹胡基亞
uj5u.com熱心網友回復:
你能更新mockito到scalatest最新版本嗎?我嘗試重現錯誤,但在我的情況下測驗通過了。你是如何禁用的mock-maker-inline?
它似乎IdiomaticMockitoFixture已被棄用,您應該使用IdiomaticMockito.
但是,添加時我確實收到了這個警告mock-maker-inline:
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
這似乎是一個無害的警告。即使我禁用了我的
我的build.sbt:
libraryDependencies = "org.scalatest" %% "scalatest" % "3.2.12" % Test
libraryDependencies = "org.scalamock" %% "scalamock" % "5.2.0" % Test
libraryDependencies = "org.scalatestplus" %% "scalacheck-1-16" % "3.2.12.0" % Test
libraryDependencies = "org.scalatestplus" %% "mockito-4-5" % "3.2.12.0" % Test
libraryDependencies = "org.mockito" % "mockito-core" % "4.6.1" % Test
libraryDependencies = "org.mockito" %% "mockito-scala" % "1.17.7" % Test
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/495264.html
