Mockito argument matchers机制
有时候要stubbing或者verify的方法有参数,但是我不关心输入的具体内容,只是完成打桩或者验证,这时候就可以用Mockito提供的argument matchers机制.
- Mockito.anyInt() 匹配任意 int 参数,
- Mockito.anyString() 匹配任意 String 参数
- Mockito.anySet()匹配任意Set
- Mockito.any() 则意味着参数为任意值
模拟参数可以用在两种情况下:
- 打桩时模拟传入的参数
- 验证时模拟传入的参数
打桩时模拟传入参数
在stubbing打桩某个方法时,如果不关心输入参数,可以用Mockito的argument matchers机制来模拟输入参数
待测试类和方法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentRepository studentRepository;
@Override
public StudentEntity findBySeqId(Stri