【发布时间】:2018-08-24 11:28:36
【问题描述】:
我们正在开发一个 Spring-Boot 应用程序,它使用 ArangoDB 和 ArangoDB 团队本身的 Spring-Data 集成 (https://github.com/arangodb/spring-data)。
我们在尝试开发集成测试时遇到了一些奇怪的问题:当我们在测试类中对不相关的 Spring 组件使用 @MockBean 注解时,无法创建应用程序上下文:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'repository': Unsatisfied dependency
expressed through constructor parameter 1: Ambiguous argument values for
parameter of type [com.arangodb.springframework.core.ArangoOperations] -
did you specify the correct bean references as arguments?
似乎仅仅存在@MockBean Annotation 就会产生这个错误。
此外,安装了 Spring Tools 的 Eclipse 在 Main 类上显示以下错误:
No constructor with 1 argument defined in class
'com.arangodb.springframework.repository.ArangoRepositoryFactoryBean'
这个错误消息似乎是真的,因为这个工厂只有一个两个参数的构造函数。无论如何,没有@MockBean 的测试确实可以成功运行。
示例
我们将项目剥离为一个最小的示例,您可以在 GitHub 上找到该示例:https://github.com/mark--/arango-mockbean-error
相关代码在单测试类DAOTest中。如果存在 @MockBean 注释,则会发生上述错误,如果将其删除,则会创建应用程序上下文。
注意:我们删除了示例项目中的测试设置代码,特别是使用 docker 设置 ArangoDB 实例:因此,在没有 @MockBean 注释的情况下测试也失败了,但这仅仅是因为失踪的 ArangoDB。
更新:
MockBean annotation in Spring Boot test causes NoUniqueBeanDefinitionException(给@MockBean 命名)的解决方法也适用于我们,所以这似乎是一些 Spring 错误。
【问题讨论】:
-
那么给@MockBean注解命名有什么问题呢?
-
因为这不是“解决方案”。作为一种解决方法,它很好,但这几乎肯定是一个错误。
-
这是一个错误,是的,但您有一个解决方法,我们无法为您修复 spring..
-
我不希望你为我修复 Spring。我在意识到这个错误之前就问了这个问题。
-
我猜在春季团队补丁之前你知道解决方案..
标签: java spring-boot testing mocking arangodb