在程序包上运行时Junit测试失败,但在文件上运行时成功

时间:2014-05-15 14:40:51

标签: java junit mockito bamboo

大更新: 有没有人遇到过这个?

我在Maven项目中使用JUnit 4.5和Mockito 1.7。

我在testCaseFolder包中有testCaseA.java。 如果我打开testCaseA.java,右键单击代码,选择"运行为" - " Junit测试"没关系。 但如果我右键单击包,请选择"运行为" - " Junit测试",它将失败:

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
}

1 个答案:

答案 0 :(得分:0)

<强>更新 从错误中得到这个应该是正确的。

你的Mockito.when陈述错了。

Mockito.when(commonService.get(Mockito.eq(contactGroupId))).thenReturn(disseminationProfile);

或者

Mockito.when(commonService.get(Mockito.anyLong())).thenReturn(disseminationProfile);