gpt4 book ai didi

java - 当 thenReturn 使用 any() 时,Mockito 出现错误 "Value must not be null"

转载 作者:行者123 更新时间:2023-12-02 09:58:10 27 4
gpt4 key购买 nike

我的单元测试中有以下代码来模拟数据库调用:

Mockito.when(valueRepository.findAllByDateBetweenAndValueContent_BoolVal(
any(LocalDate.class),
any(LocalDate.class),
anyBoolean()
)).thenReturn(new ArrayList<>());

每当我尝试运行代码时,都会收到以下错误:

org.springframework.dao.InvalidDataAccessApiUsageException: Value must not be null!; nested exception is java.lang.IllegalArgumentException: Value must not be null!

我尝试弄乱返回值和输入(任何...),但我找不到解决方案,而且我真的不明白什么值是 null/spring 提示什么。

valueRepository - 变量已正确初始化,我通过调试检查它不为空。

最佳答案

我发现错误:我使用 Autowired 而不是 MockBean 插入了 valueRepository 变量:

错误/之前:

@Autowired
ValueRepository valueRepository;

正确/之后:

@MockBean
ValueRepository valueRepository;

经过此更改,现在可以使用了。不幸的是,错误消息没有太大帮助

关于java - 当 thenReturn 使用 any() 时,Mockito 出现错误 "Value must not be null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55835327/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com