【问题标题】:How to add a protected configuration with access to properties to a Spring Boot Test context?如何将可访问属性的受保护配置添加到 Spring Boot 测试上下文?
【发布时间】:2021-10-04 11:59:29
【问题描述】:

我将 JUnit 5 与 Spring Boot 2.5.2 一起使用。现在我想编写一个单元测试,它不会加载完整的应用程序上下文。 因此我这样注释我的测试:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {ConfigurationA.class})
class Test1{
...
}

在 ConfigurationA 中创建了 Bean1。 问题是 ConfigurationA 访问 ConfigurationB 以创建 Bean1,但 ConfigurationB 受到保护。

现在我收到以下错误:

在 com.package.sample.config.ConfigurationA 中创建名称为“Bean1”的 bean 时出错:通过方法“createBean1”参数 0 表达的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的 'com.package.sample.config.ConfigurationB 类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注释:{}

当我将 @ContextConfiguration... 更改为 @SpringBootTest 时,它可以工作,但整个 Context 都会被加载。

有没有办法加载不是整个上下文?

【问题讨论】:

    标签: java spring spring-boot unit-testing spring-test


    【解决方案1】:

    只需让 ConfigurationA 扩展 ConfigurationB

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 2020-05-09
      • 2012-01-28
      • 1970-01-01
      • 2020-02-10
      • 2018-02-03
      • 2013-08-06
      • 2012-09-27
      • 1970-01-01
      相关资源
      最近更新 更多