我正在使用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)
答案 0 :(得分:2)
直接回答问题:默认操作,不使用匹配器,等同于Matchers.eq。要在使用其他匹配器(包括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类,并且每个参数使用一个匹配器)来解决您的问题。有关Hamcrest和Mockito匹配器之间差异的更多背景信息,以及每个参数需要一个匹配器,请参阅我的other SO answer。