简体   繁体   English

如何将可以访问属性的受保护配置添加到 Spring Boot Test 上下文?

[英]How to add a protected configuration with access to properties to a Spring Boot Test context?

i use JUnit 5 with Spring Boot 2.5.2 .我将 JUnit 5 与 Spring Boot 2.5.2 一起使用。 Now i want to write a Unit test, that does not load the full Application context.现在我想编写一个单元测试,它不会加载完整的应用程序上下文。 Therefore i annotate my test like that:因此,我像这样注释我的测试:

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

In ConfigurationA the Bean1 gets created.在 ConfigurationA 中,Bean1 被创建。 The Problem is that the ConfigurationA accesses an ConfigurationB for creating Bean1, but ConfigurationB is protected.问题是 ConfigurationA 访问 ConfigurationB 以创建 Bean1,但 ConfigurationB 受到保护。

Now i get the following Error:现在我收到以下错误:

Error creating bean with name 'Bean1' defined in com.package.sample.config.ConfigurationA: Unsatisfied dependency expressed through method 'createBean1' parameter 0;使用 com.package.sample.config.ConfigurationA 中定义的名称“Bean1”创建 bean 时出错:通过方法“createBean1”参数 0 表示的不满足的依赖关系; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.package.sample.config.ConfigurationB available: expected at least 1 bean which qualifies as autowire candidate.嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: Noqualifying bean of type 'com.package.sample.config.ConfigurationB available: expected at least 1 bean that qualified as autowire候选。 Dependency annotations: {}依赖注释:{}

When i change @ContextConfiguration... to @SpringBootTest it works but the whole Context gets loaded.当我将 @ContextConfiguration... 更改为 @SpringBootTest 时,它可以工作,但整个上下文都被加载了。

Is there any solution to load not the whole context?是否有任何解决方案可以不加载整个上下文?

只需让 ConfigurationA 扩展 ConfigurationB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM