Spring-boot application-test.properties

前端 未结 1 503
终归单人心
终归单人心 2020-12-10 14:37

I am trying to unit test the spring-boot application using junit. I have placed the application-test.properties under src/test/resources. I have a ApplicationConfiguration C

相关标签:
1条回答
  • 2020-12-10 15:09

    The issue is you don't have @EnableConfigurationProperties on your test class.
    When you load the application it start from main class(one which has @SpringBootApplication) where you might have @EnableConfigurationProperties and hence it works when you start the application.
    Whereas when you are running the Test with only ApplicationConfiguration class as you have specified here

    @SpringBootTest(classes=ApplicationConfiguration.class)
    

    Spring doesn't know that it has to Enable Configuration Properties and hence the fields are not injecting and hence null. But spring is reading your application-test.properties file. This can be confirmed by just injecting the value directly in your test class

    @Value("${xxxxx}")
    private String xxxxx;
    

    Here the value is injected. But to inject into a class with ConfigurationProperties you need to enable it using @EnableConfigurationProperties

    Put @EnableConfigurationProperties on your test class and everythhing works fine.

    0 讨论(0)
提交回复
热议问题