Junit test fails when run on package, but success when run on file

放肆的年华 提交于 2020-01-14 04:16:13

问题


Big update: Has anyone run into this before?

I'm using JUnit 4.5 and Mockito 1.7 in a Maven project.

I have testCaseA.java in package testCaseFolder. if I open testCaseA.java, right click in the code, select "Run as" -"Junit test" it is okay. But if I right click package, select "Run as" -"Junit test", it will fail:

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) 

Line 33 is: //MockitoAnnotations.initMocks(this);***//it said this is error***

Here is the code:

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:


Update from the error your getting this should be correct.

Your Mockito.when statement is wrong.

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

Or

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


来源:https://stackoverflow.com/questions/23681501/junit-test-fails-when-run-on-package-but-success-when-run-on-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!