为了账号安全,请及时绑定邮箱和手机立即绑定

springboot juint 测试 Could not open ServletContext resource [/application.yml]

springboot juint 测试 Could not open ServletContext resource [/application.yml]

3不戒 2018-07-02 17:14:08
springboot juint 测试 Could not open ServletContext resource [/application.yml]junit测试时一直提示Could not open ServletContext resource [/application.yml]目录结构service层测试代码@RunWith(SpringRunner.class) @SpringBootTest(classes = GirlApplication.class)//启动整个springboot功能 public class GirlServcieTest {     @Autowired     private GirlService  girlService;     @Test     public void findById(){         Girl byId = girlService.findById(1);         Assert.assertEquals(new Integer(9),byId.getAge());     } }问题:org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.springboot.GirlApplication]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/application.yml]at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:183) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:272) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525) ~[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) ~[spring-boot-1.5.14.RELEASE.jar:1.5.14.RELEASE]
查看完整描述

1 回答

?
D_cong

TA贡献1条经验 获得超3个赞

这边报错并不是因为springboot程序入口默认配置文件找不到,而是项目中存在@PropertySource注解 。如果是这样,有两种解决方案。

解决方案1:

@PropertySource("application.yml")
替换成 
@PropertySource("classpath:application.yml")

解决方案2:

@SpringBootTest(classes = GirlApplication.class)
替换成
@ContextConfiguration(
      classes = { GirlApplication.class},
    initializers = {ConfigFileApplicationContextInitializer.class}
)


查看完整回答
3 反对 回复 2018-07-18
  • 1 回答
  • 0 关注
  • 3679 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信