【发布时间】:2014-05-15 14:40:51
【问题描述】:
重大更新: 有没有人遇到过这种情况?
我在一个 Maven 项目中使用 JUnit 4.5 和 Mockito 1.7。
我在 testCaseFolder 包中有 testCaseA.java。 如果我打开testCaseA.java,在代码中右击,选择“Run as”-“Junit test”就可以了。 但是如果我右键包,选择“Run as”-“Junit test”,就会失败:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected!
Somewhere before this line you probably misused Mockito argument matchers.
For example you might have used anyObject() argument matcher outside of verification or stubbing.
Here are examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"));
at testCaseA.setUP(testCaseA.java:33)
第 33 行是://MockitoAnnotations.initMocks(this);***//it said this is error***
代码如下:
SomeService service;
@Mock
private CommonService commonService;
@Mock
public Errors errors;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);***//it said this is error***
}
@Test
public void testvalidate() {
//fail even here is empty
}
【问题讨论】:
-
经过一番挖掘,我找到了问题所在,我只是不知道为什么以及如何解决。问题不在于 MockitoAnnotations.initMocks(this);问题是mock,@Mock private CommonService commonService;如果我不嘲笑任何东西,它就会被发现。如果在这里模拟,就会出错。
-
我回滚了您的编辑,因为有一个答案与您的原始版本有关。将来,避免进行使现有问题答案无效的编辑。这可能会导致遇到类似问题的其他人对您的具体错误是什么以及您如何解决它感到困惑。