Spring boot, read yml properties via integration test case

后端 未结 5 1067
太阳男子
太阳男子 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:37

    Try this one:

    @SpringApplicationConfiguration(classes = TestBean.class, initializers = ConfigFileApplicationContextInitializer.class)
    

    From its JavaDocs:

    * {@link ApplicationContextInitializer} that can be used with the
    * {@link ContextConfiguration#initializers()} to trigger loading of
    * {@literal application.properties}.
    

    It says that it works with application.properties, but I guess it should work with application.yml as well.

    0 讨论(0)
  • 2020-12-16 21:39

    Here's how I got it to work:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(initializers=ConfigFileApplicationContextInitializer.class)
    public class MyTestClass {
    
      @Autowired
      private ConfigurableApplicationContext c;
    
      @Test
      public void myTestMethod() {            
         String value = c.getEnvironment().getProperty("myapp.property")
         ...
      }
    }
    
    0 讨论(0)
  • 2020-12-16 21:44

    If you have 'application-test.yml' in resources folder.

    You can try this:

    import org.springframework.test.context.ActiveProfiles;
    @ActiveProfiles("test")
    

    From it's Java Docs:

     * {@code ActiveProfiles} is a class-level annotation that is used to declare
     * which <em>active bean definition profiles</em> should be used when loading
     * an {@link org.springframework.context.ApplicationContext ApplicationContext}
     * for test classes.
     *
     * <p>As of Spring Framework 4.0, this annotation may be used as a
     * <em>meta-annotation</em> to create custom <em>composed annotations</em>.
    
    0 讨论(0)
  • 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]

    0 讨论(0)
  • 2020-12-16 21:54

    SpringApplicationConfiguration is deprecated in spring [Spring Boot v1.4.x] and removed in: [Spring Boot v1.5.x]. So, this is an updated answer.

    MyTestClass class is like:

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringRunner;
    import static org.assertj.core.api.Assertions.assertThat;
    
    @RunWith(SpringRunner.class)
    @ContextConfiguration(classes = MyConfiguration.class, initializers = ConfigFileApplicationContextInitializer.class)
    public class MyTestClass {
        @Autowired
        private MyYmlProperties myYmlProperties;
    
        @Test
        public void testSpringYmlProperties() {
            assertThat(myYmlProperties.getProperty()).isNotEmpty();
        }
    }
    

    MyYmlProperties class is like:

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ConfigurationProperties(prefix = "my")
    public class MyYmlProperties {
        private String property;
        public String getProperty() { return property; }
        public void setProperty(String property) { this.property = property; }
    }
    

    My application.yml is like:

    my:
      property: Hello
    

    Finally MyConfiguration is really empty :-) you can fill it with what you want:

    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @EnableConfigurationProperties(value = MyYmlProperties.class)
    public class MyConfiguration {
    }
    
    0 讨论(0)
提交回复
热议问题