Load different application.yml in SpringBoot Test

前端 未结 11 1155
天涯浪人
天涯浪人 2020-11-30 22:09

I\'m using a spring boot app which runs my src/main/resources/config/application.yml.

When I run my test case by :

@RunWith(SpringJUnit4ClassRunner.c         


        
11条回答
  •  不知归路
    2020-11-30 22:51

    See this: Spring @PropertySource using YAML

    I think the 3rd answer has what you're looking for, i.e have a separate POJO to map your yaml values into:

    @ConfigurationProperties(path="classpath:/appprops.yml", name="db")
    public class DbProperties {
        private String url;
        private String username;
        private String password;
    ...
    }
    

    Then annotate your test class with this:

    @EnableConfigurationProperties(DbProperties.class)
    public class PropertiesUsingService {
    
        @Autowired private DbProperties dbProperties;
    
    }
    

提交回复
热议问题