繁体   English   中英

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

[英]Junit test fails when run on package, but success when run on file

大更新:之前有人遇到过吗?

我在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
}

从错误更新您得到的应该是正确的。

您的Mockito.when陈述有误。

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

要么

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM