Scala Mockito 验证方法总是成功的

问题描述

我正在尝试使用 scala 作为语言来使用 mockitoverify 方法。但是,对于我的案例,即使没有调用方法,验证方法也总是成功的。

下面是 getDetailsBySkus 在内部仅调用一次方法 getPriceAndAvailability 的代码行。我怎样才能做到这一点?

下面是代码片段。

代码

doReturn(Future(TestConstantsspec.PNA_RESPONSE),Nil: _*)
      .when(distributorService).getPriceAndAvailability(ArgumentMatchers.any())(ArgumentMatchers.any())

"empty part number" should "not execute calls-1" in {


    purchaSEOrderService.getDetailsBySkus(purchaSEOrder.distributor,Seq("KJH"))
      .flatMap(output => {

        verify(distributorService,Mockito.atLeast(10))
              .getPriceAndAvailability(ArgumentMatchers.any())(ArgumentMatchers.any()) // successful

        Future(output)
      }
      )

  }

"empty part number" should "not execute calls-2" in {


    purchaSEOrderService.getDetailsBySkus(purchaSEOrder.distributor,Mockito.atLeast(100))
              .getPriceAndAvailability(ArgumentMatchers.any())(ArgumentMatchers.any()) // successful

        Future(output)
      }
      )

  }

解决方法

您的测试用例不会等待 Futures 的完成,如果您使用的是 ScalaTest 或类似工具,您可以在此处阅读您的选项:https://www.scalatest.org/user_guide/async_testing