当前位置: 首页 > 知识库问答 >
问题:

mock-maker-inline导致具有final方法特征的测试用例失败

吕越彬
2023-03-14

我们需要使用模拟制作者内联来模拟某些第三方库(例如 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后,其他使用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,那么这个测试用例可以工作。这里,在这个示例应用程序中,我们只添加了一个有问题的测试用例。

下面是示例代码:

>

  • Shape.scala

    包mockito.sample

    特征形状{

    final def printArea():Unit={println(s“Area is:$getArea()”)}

    def getArea():双精度

    }

    矩形. scala

    包mockito.sample

    类矩形(l:Long,b:Long)扩展形状{

    override def getArea(): Double = { l * b }

    }

    AreaCalculator.scala

    包mockito.sample

    类 面积计算器(形状: 形状) {

    def print area():Boolean = { shape . print area()true }

    }

    AreaCalculatorSpec.scala

    包mockito.sample

    导入org.mockito.integrations.scalatest.IdiomaticMockitoFixture

    import org.scalatest.concurrent.ScalaFutures

    import org.scalatest.matchers.must.Matchers.convertToAnyMustWrapper

    导入org.scalatest.matchers.should.matcheers

    导入org . Scala test . wordspec . anywordspec

    org.scalatest.导入

    导入org.scalatestplus.scalacheck.scalaccheckPropertyChecks

    类AreaCalculatorSpec使用带有ScalaFutures的匹配器扩展AnyWordSpec,带有TryValues的匹配器带有ScalaCheckPropertyChecks {

    trait Setup { val rectangle = mock[Rectangle] val areaCalculator = new AreaCalculator(rectangle) }

    " AreaCalculator#printArea "应该{ "应该调用适当形状的区域"在新设置中{ rectangle.getArea()应该返回40.0 areaCalculator.printArea()必须为true } }

    }

    请检查并提出您的宝贵意见。如果需要任何其他细节,请告诉我。

    谢谢你,

    Rakesh Dhandhukiya

  • 共有1个答案

    卢深
    2023-03-14

    能否将<code>mockito?

    似乎<code>IlimaticMockitoFixture。

    但是,在内联添加<code>mock maker时,我确实收到了此警告。<code>:

    OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
    

    这似乎是一个无害的警告。即使我禁用了我的仪器仪表代理,我仍然得到它。

    如果我删除< code>mock-maker-inline,警告消失。

    这是代码,有点修改:

    trait Shape {
      final def printArea(): Unit = println(s"Area is: $getArea")
      def getArea: Double
    }
    
    class Rectangle(l: Long, b: Long) extends Shape {
      override def getArea: Double = l * b
    }
    
    class AreaCalculator(shape: Shape) {
      def printArea(): Boolean = {
        shape.printArea()
        true
      }
    }
    

    < code > arecalculatorspec 更新了新的依赖关系:

    import org.mockito.IdiomaticMockito
    import org.scalatest.concurrent.ScalaFutures
    import org.scalatest.matchers.must.Matchers.convertToAnyMustWrapper
    import org.scalatest.matchers.should.Matchers
    import org.scalatest.wordspec.AnyWordSpec
    import org.scalatest.{EitherValues, TryValues}
    import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
    
    class AreaCalculatorSpec
        extends AnyWordSpec
        with Matchers
        with ScalaFutures
        with EitherValues
        with TryValues
        with IdiomaticMockito
        with ScalaCheckPropertyChecks {
    
      trait Setup {
        val rectangle: Rectangle = mock[Rectangle]
        val areaCalculator = new AreaCalculator(rectangle)
      }
    
      "AreaCalculator#printArea" should {
        "should invoke area of the appropriate shape" in new Setup {
          rectangle.getArea shouldReturn 40.0
          areaCalculator.printArea() mustBe true
        }
      }
    }
    

    我的模拟制造商内联设置:

    我的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
    
     类似资料:
    • 我试图模拟最后一个类的静态方法:Files。以身作则。 我读到我必须用户一个mock制造商内联mockito插件,所以我设置它: 我把这个: 在:src/test/Resources/mockito扩展/org.mockito.plugins.MockMaker 然后我模拟了我的类并尝试验证我的模拟是否被调用: 我有一个例外: 我在mockito 2.7.22中使用了power mockito 2

    • 下面TestNG执行测试用例会导致失败。错误-org.openqa.selenium.WebDriverExcture:在处理命令时发生了未知的服务器端错误。原始错误:无法将命令代理到远程服务器。原始错误:错误:套接字挂断(警告:服务器没有提供任何堆栈跟踪信息)命令持续时间或超时: 56毫秒

    • 我有这两种方法,在使用Mock实现测试时遇到了困难。我该如何参加考试? 我有麻烦得到Jboss目录而不必启动System.get属性(jboss.server.temp.dir);

    • 我有一个测试类,其中我有大约10个单元测试用例。在本文中,我创建了一个mock方法,它只在10个测试用例中的2个中有用。 因此,从技术上讲,我希望对8个测试用例执行方法,并对其中2个测试用例进行模拟。 有没有办法做到这一点?

    • 我已经为Jenkins项目的集成公司建立了一个PoC,当Git存储库中有一个检查时,该PoC将构建并执行测试。当测试完成后,Jenkins将更新在Xray for JIRA中管理的测试。 测试是用cucumber写的。我曾徒劳地试图导致一个单一的测试产生一个失败,只是为了能够将它添加到我将要给高层管理人员的演示中。 以下是我的文件helloworld.feature的内容: 我知道不知道小cucu

    • 问题内容: Django文档(http://docs.djangoproject.com/zh-CN/1.3/topics/testing/#running-tests)指出,你可以通过指定单个测试用例来运行它们: 假设你将测试保存在Django应用程序的tests.py文件中。如果是这样,那么此命令将按预期工作。 我在tests目录中有针对Django应用程序的测试: 该文件具有函数: 要运行测