问题描述
我想在测试中注入DeMorgenArticleScraper.
I want to inject DeMorgenArticleScraper in a test.
@RunWith(SpringJUnit4ClassRunner.class)
public class DeMorgenArticleScraperTest {
@Autowired
private DeMorgenArticleScraper deMorgenArticleScraper;
...
}
DeMorgenArticleScraper组件本身具有一些配置,但是IDE/编译器没有抱怨它们.
The DeMorgenArticleScraper component has some configuration going on for itself, but the IDE/compiler is not complaining about them.
@Component
public class DeMorgenArticleScraper extends NewsPaperArticleScraper {
@Autowired
public DeMorgenArticleScraper(
@Qualifier("deMorgenSelectorContainer") SelectorContainer selector,
GenericArticleScraper genericArticleScraper,
@Qualifier("deMorgenCompany") Company company) {
super(selector, genericArticleScraper, company);
}
...
}
用@Qualifier注释的构造函数参数在具有@Bean的Config.class中定义.该类本身具有@Configuration.我认为问题不在这里.
The constructor parameters that are annotated with @Qualifier, are defined in a Config.class With @Bean. The class itself has @Configuration. I figure the problem is not situated here.
IDE已经警告我,没有找到bean ...必须在bean中定义自动装配的成员.但据我所知,它是在具有@Component批注的bean中定义的.当Spring Boot应用程序可以启动时(当我注释掉测试类时),所有其他bean连接似乎都没问题.
The IDE warns me already, no bean found...autowired members must be defined in a bean. But as far as I know, it is defined in a bean with the @Component annotation. All other bean wiring seems ok as the Spring boot application can start (when I comment out the test class).
推荐答案
@SpringBootTest
相当重量级,出于所有意图和目的,将加载整个应用程序, https://docs.spring.io/spring -boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications ,它的重量很大,并且会极大地影响测试时间.根据您要测试的内容,您可能需要研究
@SpringBootTest
is fairly heavyweight, and for all intents and purpose will load your entire application, https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications, it's fairly heavyweight and dramatically affects test time. Depending on what you are trying to test you may want to look into
- 切片测试,例如
@JsonTest
,@DataJpaTest
,@WebMvcTest
等, https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing .html#boot-features-testing-spring-boot-applications-testing-autoconfigured-tests .这些测试的好处不仅在于它们不会加载所有内容,从而更快地加载所有内容,而且还会尝试找出相关的配置. - 使用旧的
@ContextConfiguration
并指向相关的@Configuration
,以加载测试 https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#contextconfiguration
- Slice tests e.g.
@JsonTest
,@DataJpaTest
,@WebMvcTest
etc. , https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-tests. Benefit of these tests are not only will they not load everything, thus faster, but will try to hunt out the relevant configurations. - Plain old
@ContextConfiguration
and point to the relevant@Configuration
's required to load beans needed for the test https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#contextconfiguration
这篇关于使用autowire的Spring Boot字段注入在JUnit测试中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!