How to test Classes with @ConfigurationProperties and @Autowired

后端 未结 5 924
感情败类
感情败类 2020-12-09 07:52

I want to test small parts of the application that rely on properties loaded with @Autowired and @ConfigurationProperties. I am looking for a solut

5条回答
  •  臣服心动
    2020-12-09 08:00

    You need to annotate your TestConfiguraion with @EnableConfigurationProperties as follows:

    @EnableConfigurationProperties
    public class TestConfiguration {
    
        @Bean
        @ConfigurationProperties(prefix = "test")
        public TestSettings settings (){
            return new TestSettings();
        }
    }
    

    Also you only need to include TestConfiguration.class in @ContextConfiguration of you SettingsTest class:

    @TestPropertySource(locations = "/SettingsTest.properties")
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = TestConfiguration.class)
    public class SettingsTest {
    ...
    

提交回复
热议问题