spring-boot - 为什么@MockBean ConversionService 会导致错误
问题描述
给定:使用 Intellij IDEA Ultimate 创建的 JUnit 5 和空 SpringBoot 项目。对于带有包含@Autowired ConversionService 的控制器的项目,我得到了相同的结果。
我需要在测试中使用 ConversionService 的模拟版本。
这是我的方法:
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.convert.ConversionService;
@SpringBootTest
public class ConversionServiceTest {
@MockBean
ConversionService conversionService;
@Test
void test() {}
}
上面的代码导致错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.format.support.FormattingConversionService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value="mvcConversionService")}
此错误的原因是什么,解决方案是什么?
解决方案
解决方案是用@SpyBean 替换@MockBean。
推荐阅读
- wordpress - 在 WooCommerce 中创建新用户后,用户更改不起作用
- java - 如何更改 JavaFx ComboBox 文本颜色
- java - StringJoiner - 括号中的每一项
- python - 使用字典一次设置 pandas 行的值
- javascript - Facebook 调试器不拾取 Next.js 的 next-seo 元标记
- python - GroupConcat 在 Django 2.x 中工作。在 Django 3.1 中抛出“无法解析表达式类型,未知 output_field”
- java - 二叉搜索树java:为什么remove方法绑定到一个节点?
- php - Codeigniter 点击带有 id 的 url 来查看页面
- windows-10 - NtQueryVirtualMemory 不适用于 64 位
- python - 使用 Jupyter 笔记本中的参数运行 Windows 命令行