bil*_*ill 3 java spring junit5 spring-boot-test
我的问题:如果我的测试引用@Bean中列出的类中的声明,则@SpringBootTest自动装配工作。如果它引用@ComponentScan由 中列出的类自动连接的类@SpringBootTest,则自动装配失败。在测试之外,我的应用程序启动时没有自动装配或组件扫描问题,并且我可以确认我要在测试中加载的服务在非测试中运行良好。我很沮丧。我坏了,还是 Spring Boot 2 上的 Junit5 功能?
我的测试:
@ExtendWith(SpringExtension.class)
@SpringBootTest (classes=MyConfig.class)
public class MyTest {
// fails to autowire
@Autowired
private MyService _mySvc ;
// succeeds!
@Autowired @Qualifier ( "wtf" )
private String _wtf ;
Run Code Online (Sandbox Code Playgroud)
我的配置:
@EnableWebMvc
@SpringBootApplication ( scanBasePackages = "my.packaging" )
@Configuration
public class MyConfig {
@Bean
public String wtf ( ) { return "W T F???" ; }
// No @Bean for MyService because component scan is nicer in the non-test world
Run Code Online (Sandbox Code Playgroud)
小智 7
尽管测试开始了,但我遇到了与自动装配不起作用的相同问题,问题是我仍在使用旧的 junit4 @Test 注释。确保您的测试方法使用 juni5 包 org.junit.jupiter.api.Test 中的 @Test 进行注释。
| 归档时间: |
|
| 查看次数: |
5944 次 |
| 最近记录: |