我的specs2测试之一出现错误。该项目通过Play!Framework(Scala)实现。

这是我的测试:

class AmazonEmailSenderSpec extends Specification with Mockito {
    "AmazonEmailSenderSpec#sendEmail" should {
        "Send Email with correct data" in {

          val client = mock[AmazonSimpleEmailServiceAsyncClient]
          val emailSender = new AmazonEmailSender(client)

          emailSender.sendEmail(Email(subject, content, from, to, cc))

          val captor = ArgumentCaptor.forClass(classOf[SendEmailRequest])
          there was one(client).sendEmail(captor.capture)

          val sendEmailRequest: SendEmailRequest = captor.getValue
        }
      }
}

如果我在intellij上运行该测试,效果很好,但是以某种方式,当我使用play activator在控制台上运行它时,它失败了:
activator test

我得到:
[info] AmazonEmailSenderSpec#sendEmail should
[error]   ! Send Email with correct data
[error]    org.mockito.internal.matchers.LocalizedMatcher cannot be cast to org.mockito.ArgumentMatcher (ArgumentsComparator.java:25)
[error] org.mockito.internal.invocation.ArgumentsComparator.argumentsMatch(ArgumentsComparator.java:25)
[error] org.mockito.internal.invocation.ArgumentsComparator.argumentsMatch(ArgumentsComparator.java:17)
[error] org.mockito.internal.invocation.InvocationMatcher.matches(InvocationMatcher.java:56)
[error] org.mockito.internal.invocation.InvocationsFinder$RemoveNotMatching.isOut(InvocationsFinder.java:145)
[error] org.mockito.internal.invocation.InvocationsFinder$RemoveNotMatching.isOut(InvocationsFinder.java:137)
[error] org.mockito.internal.util.collections.ListUtil.filter(ListUtil.java:16)
[error] org.mockito.internal.invocation.InvocationsFinder.findInvocations(InvocationsFinder.java:20)
[error] org.mockito.internal.verification.checkers.MissingInvocationChecker.check(MissingInvocationChecker.java:32)
[error] org.mockito.internal.verification.Times.verify(Times.java:36)
[error] org.mockito.internal.verification.MockAwareVerificationMode.verify(MockAwareVerificationMode.java:21)
[error] org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:76)
[error] org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29)
[error] org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:37)
[error] org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:36)
[error] org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.access$000(MockMethodInterceptor.java:17)
[error] org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$DispatcherDefaultingToRealMethod.interceptSuperCallable(MockMethodInterceptor.java:96)
[error] com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceAsyncClient$MockitoMock$1778874818.sendEmail(Unknown Source)
[error] integration.email.AmazonEmailSenderSpec$$anonfun$1$$anonfun$apply$11$$anonfun$apply$12.apply(AmazonEmailSenderSpec.scala:27)
[error] integration.email.AmazonEmailSenderSpec$$anonfun$1$$anonfun$apply$11$$anonfun$apply$12.apply(AmazonEmailSenderSpec.scala:27)
[error] integration.email.AmazonEmailSenderSpec$$anonfun$1$$anonfun$apply$11.apply(AmazonEmailSenderSpec.scala:27)
[error] integration.email.AmazonEmailSenderSpec$$anonfun$1$$anonfun$apply$11.apply(AmazonEmailSenderSpec.scala:19)

我不明白为什么应该改为使用ArgumentMatcher时激活器使用LocalizedMatcher。

我尝试匹配的方法的签名是:
public SendEmailResult sendEmail(SendEmailRequest sendEmailRequest)

在Google上找不到任何提示。有任何想法吗?我是否真的错过了一些愚蠢的事情?

最佳答案

这是一个老问题,但我在2017年碰巧遇到了同一问题:

一个对我有用的解决方案(在Java / Scala混合项目中的JUnit / Specs2):

我强迫模仿者使用最新版本的2.7.11。但是我也依赖org.specs2:specs2-mock_2.12:3.8.6,它仍然基于模仿1.9.5。参见maven central
问题就在这里。我删除了对specs2-mock和Specs2的Mockito语法糖的依赖,并将其替换为简单的Mockito代码。瞧!上面提到的错误消失了。

10-06 11:27