我正在尝试使用Mockito来模拟服务,但是它说我没有正确使用参数Mather。
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)
,但是没有办法测试我的发送方法是否在MessageCreator
中没有创建实际对象的情况下执行?
答案 0 :(得分:0)
问题出在以下几行:
when(tableReferenceService.getTable(anyLong())).thenReturn(anyObject());
您无法使用匹配器作为预期的返回值。你想要的可能是:
when(tableReferenceService.getTable(anyLong())).thenReturn(new Table());
或
Table table = mock(Table.class);
when(tableReferenceService.getTable(anyLong())).thenReturn(table);
取决于tableReferenceService.getTable()
返回的对象是否需要嘲笑。