Java 获取org.mockito.exceptions.misusing.invalidUseofMatcherException异常,即使我正确使用了Mather
我试图用Mockito模拟一个服务,但它说我没有正确地使用参数MatherJava 获取org.mockito.exceptions.misusing.invalidUseofMatcherException异常,即使我正确使用了Mather,java,mocking,mockito,Java,Mocking,Mockito,我试图用Mockito模拟一个服务,但它说我没有正确地使用参数Mather import com.spmsoftware.appframework.validation.ValidationContext; import com.spmsoftware.appframework.validation.ValidationContextSupport; import com.spmsoftware.filters.api.model.Filter; import com.spmsoftware.f
import com.spmsoftware.appframework.validation.ValidationContext;
import com.spmsoftware.appframework.validation.ValidationContextSupport;
import com.spmsoftware.filters.api.model.Filter;
import com.spmsoftware.filters.api.validation.FilterValidationService;
import com.spmsoftware.tablereference.api.service.TableReferenceService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.when;
public class TestInputTabValidator {
@Mock
private Filter filter;
@Mock
private TableReferenceService tableReferenceService;
@Mock
private FilterValidationService filterValidationService;
@InjectMocks private InputTabValidator inputTabValidator;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
preCondition();
}
private void preCondition() {
when(tableReferenceService.getTable(anyLong())).thenReturn(anyObject());
when(tableReferenceService.tableExists(1L)).thenReturn(true);
}
@Test
public void validateInputTabSaveWithFilter() {
ValidationContext validationContext = new ValidationContextSupport();
inputTabValidator.validate(new Object[]{anyLong(), filter}, validationContext);
Assert.assertTrue("no validation errors recorded", !validationContext.hasErrors());
}
public void validateInputTabSaveWithoutFilter() {
ValidationContext validationContext = new ValidationContextSupport();
inputTabValidator.validate(new Object[]{anyLong()}, validationContext);
Assert.assertTrue("no validation errors recorded", !validationContext.hasErrors());
}
}
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
1 matchers expected, 2 recorded:
-> at com.spmsoftware.genericWebservices.validator.TestInputTabValidator.preCondition(TestInputTabValidator.java:55)
-> at com.spmsoftware.genericWebservices.validator.TestInputTabValidator.preCondition(TestInputTabValidator.java:56)
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 com.spmsoftware.genericWebservices.validator.TestInputTabValidator.preCondition(TestInputTabValidator.java:56)
at com.spmsoftware.genericWebservices.validator.TestInputTabValidator.setUp(TestInputTabValidator.java:51)
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:498)
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.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
原因是由于
Mockito.any(MessageCreator.class)
,但是没有办法测试我的send方法在执行时没有在MessageCreator
中创建实际对象吗?问题在下面一行:
when(tableReferenceService.getTable(anyLong())).thenReturn(anyObject());
不能将匹配器用作预期的返回值。您想要的可能是:
when(tableReferenceService.getTable(anyLong())).thenReturn(new Table());
或
取决于tableReferenceService.getTable()
返回的对象是否需要模拟 “原因是由于Mockito.any(MessageCreator.class)…”这不在您发布的代码中。
Table table = mock(Table.class);
when(tableReferenceService.getTable(anyLong())).thenReturn(table);