微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;

原文地址:https://www.jb51.cc/java/125512.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐