简体   繁体   中英

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 . 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. The Problem is that the ConfigurationA accesses an ConfigurationB for creating Bean1, but ConfigurationB is protected.

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; 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. Dependency annotations: {}

When i change @ContextConfiguration... to @SpringBootTest it works but the whole Context gets loaded.

Is there any solution to load not the whole context?

只需让 ConfigurationA 扩展 ConfigurationB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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