Spring Boot @Import用于加载配置集成测试不起作用

时间:2020-07-04 06:46:11

标签: spring spring-boot

我有一个简单的spring boot应用程序,正在实施一些集成测试。我有2个类,一个将保存我的通用配置(Demo3ApplicationTests),另一个是我的集成测试类(DumyClassTest),请在下面找到它暂时为空:

@SpringBootTest(classes = Demo3Application.class)
class Demo3ApplicationTests {

    @Test
    void contextLoads() {
        
    }

}

我的综合测试班:

@Import(value = Demo3ApplicationTests.class)
public class DumyClassTest{
    
    
    
    @Autowired
    DemoService demoService;
    
    @Test
    public void testImportConfig() {
        
        demoService.logDummyMsg();
        
    }

}

当我运行测试testImportConfig时,demoService值为null,因为我猜@import我没有正确设置它。但是,当我扩展为DumyClassTest extends Demo3ApplicationTests时,demoService不为null,并且测试运行正确。

有人知道为什么当我使用导入批注demoService为null吗?

谢谢。

0 个答案:

没有答案