springboottest 红_SpringBootTest 测试的一个问题

我的工程是个 maven 管理的 springboot 项目,有个启动类如下

@EnableApolloConfig

@SpringBootApplication

@PropertySource

@EnableSwagger2

@EnableScheduling

@ImportResource({"classpath:spring/spring-context.xml"})

public class CouponApplication extends SpringBootServletInitializer {

public static void main(String[] args) {

SpringApplication.run(CouponApplication.class);

}

}

@ImportResource 导入了 spring-context.xml 文件,这是总的 xml 文件,里面导入年其它 xml 文件

我现在想要测试,我自己又创建了一个启动类如下:

@SpringBootApplication

@ImportResource("classpath:spring/spring-datasource.xml")

@ComponentScan("...")

@EnableApolloConfig({"jdbc", "config"})

@Import({BeanConfig.class, DataSourceConfig.class, RedisConfig.class})

@OverrideAutoConfiguration(enabled = false)

public class CouponTplServiceImplTestApplication {

}

@ImportResource 中只导入了和数据库相关的 xml.

以下是我的测试类

@SpringBootTest(classes = CouponTplServiceImplTestApplication.class)

@RunWith(SpringRunner.class)

@MockBean({HttpClientUtils.class, AppInstanceRpc.class})

public class CouponTplServiceImplTest {}

我在测试类里制定的启动类是我自己创建的启动类,并不是项目中的.因此,启动类要加载的 xml 文件应该是 spring/spring-datasource.xml.但实际情况却不是.启动测试的时候,报错如下:

Caused by: java.io.FileNotFoundException: class path resource [spring/spring-context.xml] cannot be opened because it does not exist

at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)

at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)

... 45 more

错误中能看到 FileNotFoundException: class path resource [spring/spring-context.xml] . 这是因为我故意把 resouces/spring/ 目录下的其它 xml 给移除了. 从结果看,好像是 @SpringBootTest(classes = CouponTplServiceImplTestApplication.class) 没有起作用. 大佬看下这是为什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值