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

when(candidateService.findById(1)).thenReturn(new Candidate()); 我想扩展这种行为为任何整数(不一定为1) 如果我吃惊 when(candidateService.findById( any(Integer.class) )).thenReturn(new Candidate()); 我有编译错误 The method findByI

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;

作者: dawei

【声明】:永州站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部