Junit测试在程序包上运行时失败,但在文件上运行时成功 [英] Junit test fails when run on package, but success when run on file

查看:335
本文介绍了Junit测试在程序包上运行时失败,但在文件上运行时成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Big update: Has anyone run into this before?

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

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

我在testCaseFolder软件包中有testCaseA.java. 如果我打开testCaseA.java,右键单击代码,选择运行方式"-"Junit测试"就可以了. 但是,如果我右键单击程序包,选择运行方式"-"Junit测试",它将失败:

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) 

第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
}

推荐答案

更新 从错误中得到的信息应该是正确的.

Update from the error your getting this should be correct.

您的Mockito.当陈述错误时.

Your Mockito.when statement is wrong.

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

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

这篇关于Junit测试在程序包上运行时失败,但在文件上运行时成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆