java - Mockito @SpyBean 注解与 SpringRunner 冲突
问题描述
我目前正在尝试创建存储库的部分模拟,但是每当我尝试使用 SpyBean 注释 JDBC 存储库时都会遇到问题。
@RunsWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = ServiceApplication.class)
@ActiveProfiles("unit-test")
public abstract class TestBase {
@SpyBean
protected JdbcRepository jdbcRepository;
@Before
public void baseSetUp() throws Exception {
MockitoAnnotations.initMocks(this);
this.testContextManager = new TestContextManager(getClass());
this.testContextManager.prepareTestInstance(this);
this.classLoader = getClass().getClassLoader();
...
}
当我运行应用程序时,我收到以下消息:
org.springframework.beans.factory.BeanCreationException:无法注入字段:受保护的 com.sample.app.JdbcRepository 不能有现有值
但是,我找到了一些避免错误的方法:
- 删除“@RunsWith(SpringRunner.class)”后,测试似乎运行良好。但是,我阅读的许多文档似乎表明这是必需的
- 添加了“@SpyBean(JdbcRepository.class)”并且自动装配 jdbcRepository 似乎也有效。也许这是一个上下文问题,它试图多次实例化 JdbcRepository 的实例?
简而言之,我不确定为什么我的测试会抛出 BeanCreationException。类成员上方的@SpyBean注解不是使用spy beans的正确方法吗?
解决方案
推荐阅读
- c# - concat 聚合 mongo db 驱动程序 c#
- oracle - NUMBER 相当于雪花中的 Oracle
- javascript - Three.js:GLTF 加载但未出现
- javascript - 在 Firefox 的 content.js 中调用 browser.storage.local.get() 后无法 console.log()
- spring - 如何将自定义标头转发到 Spring Zuul 代理中的下游服务?
- linear-regression - 多元线性回归的事后功效分析
- azure - 如何监视 Azure 存储容器/子文件夹中 Blob 的创建并触发逻辑应用发送电子邮件
- amazon-web-services - AWS JS SDK:添加规则后的奇怪行为 GetBucketLifecycleConfiguration
- angular - 后页的角度路由转换问题
- mongodb - MongoDB:如何对来自两个不同来源的数据进行分页?