我使用的是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$1.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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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$1.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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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$1.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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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
相反,这表明您使用了错误的Matchers类。您已经导入了org.hamcrest.Matchers
,其中对Matchers.any(Class<T> clazz)
的调用返回org.hamcrest.core.IsInstanceOf
的实例。相反,您希望调用org.mockito.Matchers.any(Class<T> clazz)
,它似乎返回了T
(以避免您的演员阵容),但实际上通过副作用起作用。
您需要解决这两个问题(导入正确的Matchers类,并为每个参数使用一个matcher)来解决问题。有关Hamcrest和Mockito匹配器之间的区别,以及每个参数需要一个匹配器的更多上下文,请参阅我的另一个SO答案。