org.mockito.internal.matchers.LocalizedMatcher在运行“激活器测试”时无法使用“任何”匹配器强制转换为org.mockito.ArgumentMatcher

时间:2016-01-18 12:22:59

标签: scala mockito specs2

我在我的一个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 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)

无法在谷歌上找到任何提示。有任何想法吗?我错过了一些非常愚蠢的东西吗?

1 个答案:

答案 0 :(得分:1)

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

适用于我的解决方案(JUnit / Specs2,在混合Java / Scala中)项目:

我强迫mockito使用最新版本states[1]。但我也依赖于2.7.11仍然基于mockito org.specs2:specs2-mock_2.12:3.8.6。见maven central。 这里的某个地方存在问题。我删除了对1.9.5和Specs2的mockito语法糖的依赖,并将其替换为普通的mockito代码。瞧!上面提到的错误消失了。