Spring boot, read yml properties via integration test case

后端 未结 5 1077
太阳男子
太阳男子 2020-12-16 21:08

Hi I am using Spring Boot, I want to inject the values of the .yml file in the Bean. I have written the integration test case but looks like via Integration test case it not

5条回答
  •  误落风尘
    2020-12-16 21:44

    Here's another way: [Spring Boot v1.4.x]

    import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @BootstrapWith(SpringBootTestContextBootstrapper.class)
    public class CassandraClientTest {
    
      @Autowired
      private TestBean bean;
    
      @Test
      public void test() {
        bean.print();
      }
    }
    

    This works ONLY if also 'application.properties' file exists.

    e.g. maven project:

    src/main/resources/application.properties [ The file can be empty but it's mandatory! ]
    src/main/resources/application.yml [here's your real config file]

提交回复
热议问题