如何使用 Mockito 在方法调用中一般只匹配一个参数?

How can I generically match only one argument in a method call using Mockito?

我正在使用 JUnit 4.11 和 Mockito 1.9.5。我如何匹配方法调用中的所有参数,除了一个我不关心它是否一般匹配的参数?这是我的

Mockito.verify(m_emailSvc).sendEmail(user.getAddress().getEmail(), fromEmail, subject, “template.vm", paramMap, (File) Matchers.any(File.class));

请注意,最后一个参数(java.io.File 类型)是我尝试进行一般匹配的参数。不幸的是,在进行此调用时,我得到了 运行 时间异常

java.lang.ClassCastException: org.hamcrest.core.IsInstanceOf cannot be cast to java.io.File
    at org.mainco.subco.myproject.service.TrainingSessionServiceDWRIT.testAddCEUCreditsCompleteTraining(TrainingSessionServiceDWRIT.java:394)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

一般只匹配一个参数的正确方法是什么?如果这需要升级,我没问题。

编辑:

针对给出的答案,这里是 运行JUnit 测试时发生的新异常。这可能会触及我的问题的核心——我如何将通用匹配器与特定匹配器结合起来。

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
6 matchers expected, 2 recorded:
-> at org.mainco.subco.myproject.service.TrainingSessionServiceDWRIT.testAddCEUCreditsCompleteTraining(TrainingSessionServiceDWRIT.java:394)
-> at org.mainco.subco.myproject.service.TrainingSessionServiceDWRIT.testAddCEUCreditsCompleteTraining(TrainingSessionServiceDWRIT.java:394)

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

For more info see javadoc for Matchers class.

    at org.mainco.subco.myproject.service.TrainingSessionServiceDWRIT.testAddCEUCreditsCompleteTraining(TrainingSessionServiceDWRIT.java:394)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

编辑 2: 好吧,我把所有东西都包裹在 Matchers.eq 调用中……

            Mockito.verify(m_emailSvc).sendEmail(Matchers.eq(user.getAddress().getEmail()), 
                                                 Matchers.eq(fromEmail), 
                                                 Matchers.eq(pdCompletionSubject), 
                                                 Matchers.eq("send_pd_registration_completion.vm"), 
                                                 Matchers.eq(paramMap), 
                                                 Matchers.eq(Matchers.any(File.class)));

但仍然得到以下异常

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
6 matchers expected, 7 recorded:
-> at org.mainco.springboard.myproject.service.TrainingSessionServiceDWRIT.testAddCEUCreditsCompleteTraining(TrainingSessionServiceDWRIT.java:394)
-> at org.mainco.springboard.myproject.service.TrainingSessionServiceDWRIT.testAddCEUCreditsCompleteTraining(TrainingSessionServiceDWRIT.java:394)
-> at org.mainco.springboard.myproject.service.TrainingSessionServiceDWRIT.testAddCEUCreditsCompleteTraining(TrainingSessionServiceDWRIT.java:394)
-> at org.mainco.springboard.myproject.service.TrainingSessionServiceDWRIT.testAddCEUCreditsCompleteTraining(TrainingSessionServiceDWRIT.java:394)
-> at org.mainco.springboard.myproject.service.TrainingSessionServiceDWRIT.testAddCEUCreditsCompleteTraining(TrainingSessionServiceDWRIT.java:394)
-> at org.mainco.springboard.myproject.service.TrainingSessionServiceDWRIT.testAddCEUCreditsCompleteTraining(TrainingSessionServiceDWRIT.java:394)
-> at org.mainco.springboard.myproject.service.TrainingSessionServiceDWRIT.testAddCEUCreditsCompleteTraining(TrainingSessionServiceDWRIT.java:394)

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

For more info see javadoc for Matchers class.

    at org.mainco.springboard.myproject.service.TrainingSessionServiceDWRIT.testAddCEUCreditsCompleteTraining(TrainingSessionServiceDWRIT.java:394)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

直接回答问题:不使用 Matchers 的默认操作等同于 Matchers.eq。 在使用其他 Matchers 时保留该操作,包括 any,你可以显式调用Matchers.eq

此时,您的调用将如下所示:

// N.B. Matchers is actually org.mockito.Matchers!
Mockito.verify(m_emailSvc).sendEmail(
    Matchers.eq(user.getAddress().getEmail()),
    Matchers.eq(fromEmail),
    Matchers.eq(subject),
    Matchers.eq("template.vm"),
    Matchers.eq(paramMap),
    Matchers.any(File.class));

不过,以上错误信息的责任不大:

java.lang.ClassCastException: org.hamcrest.core.IsInstanceOf cannot be cast to java.io.File

相反,这表明您使用了错误的匹配器 class。您已导入 org.hamcrest.Matchers,其中对 Matchers.any(Class<T> clazz) return 的调用是 org.hamcrest.core.IsInstanceOf 的一个实例。相反,您想调用 org.mockito.Matchers.any(Class<T> clazz),它看起来 return 为 T(以避免您的转换)但实际上通过副作用起作用。


您需要解决这两个问题(导入正确的匹配器 class 并为每个参数使用一个匹配器)来解决您的问题。有关 Hamcrest 和 Mockito 匹配器之间差异的更多上下文,以及每个参数需要一个匹配器,请参阅我的 other SO answer.