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

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

junit测试时一直提示Could not open ServletContext resource [/application.yml]

目录结构

https://img4.mukewang.com/5b38a268000147b505890556.jpg

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]


3不戒
浏览 3680回答 1
1回答

D_cong

这边报错并不是因为springboot程序入口默认配置文件找不到,而是项目中存在@PropertySource注解 。如果是这样,有两种解决方案。解决方案1:@PropertySource("application.yml") 替换成  @PropertySource("classpath:application.yml")解决方案2:@SpringBootTest(classes = GirlApplication.class) 替换成 @ContextConfiguration(       classes = { GirlApplication.class},     initializers = {ConfigFileApplicationContextInitializer.class} )
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
测试