java – 为任何整数输入参数设置模拟返回值

when(candidateService.findById(1)).thenReturn(new Candidate());

我想扩展这种行为为任何整数(不一定为1)

如果我吃惊

when(candidateService.findById( any(Integer.class)  )).thenReturn(new Candidate());

我有编译错误

The method findById(Integer) in the type CandidateService is not
applicable for the arguments (Matcher)

UPDATE

进口:

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.mockmvcResultMatchers.status;

import java.util.ArrayList;
import java.util.HashSet;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

解决方法

尝试anyInt():
when(candidateService.findById( anyInt())).thenReturn(new Candidate());

例如我的项目中有anyLong():

when(dao.getAddress(anyLong())).thenReturn(Arrays.asList(dto));

编辑:
您必须导入:

import static org.mockito.Matchers.anyInt;

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...