Spring boot,通过集成测试用例读取yml属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27390085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:15:22  来源:igfitidea点击:

Spring boot, read yml properties via integration test case

springspring-boot

提问by plzdontkillme

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 injecting the values.

嗨,我正在使用 Spring Boot,我想在 Bean 中注入 .yml 文件的值。我已经编写了集成测试用例,但看起来通过集成测试用例它没有注入值。

Problem is value of urls and keyspaceApp is null

问题是 urls 和 keyspaceApp 的值为空

Bean

豆角,扁豆

    @ConfigurationProperties(prefix="cassandra")
public class TestBean {

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

    @Value("${keyspaceApp}")
    private String app;

    public void print() {
        System.out.println(urls);
        System.out.println(app);
    }

    public String getUrls() {
        return urls;
    }

    public void setUrls(String urls) {
        this.urls = urls;
    }
}

Integration Test case

集成测试用例

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestBean.class)
@IntegrationTest
public class CassandraClientTest {

    @Autowired
    private TestBean bean;

    @Test
    public void test() {
        bean.print();
    }
}

Application yml file

应用程序 yml 文件

cassandra:
  urls: lllaaa.com
  keyspaceApp: customer
  createDevKeyspace: true

采纳答案by Artem Bilan

Try this one:

试试这个:

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

From its JavaDocs:

从它的 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.ymlas well.

它说它适用于application.properties,但我想它也应该适用application.yml

回答by acohen

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")
     ...
  }
}

回答by Abhilekh Singh

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

如果资源文件夹中有“application-test.yml”。

You can try this:

你可以试试这个:

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

From it's Java Docs:

从它的Java文档:

 * {@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>.

回答by Ahmed Hassanien

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

SpringApplicationConfiguration在 spring [Spring Boot v1.4.x] 中被弃用,并在:[Spring Boot v1.5.x] 中删除。所以,这是一个更新的答案。

MyTestClassclass is like:

MyTestClass类是这样的:

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();
    }
}

MyYmlPropertiesclass is like:

MyYmlProperties类是这样的:

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.ymlis like:

application.yml的就像:

my:
  property: Hello

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

最后MyConfiguration真的是空的 :-) 你可以用你想要的东西填充它:

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(value = MyYmlProperties.class)
public class MyConfiguration {
}

回答by Juha Hanka

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

这是另一种方式:[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.

这仅在“application.properties”文件存在时才有效。

e.g. maven project:

例如 Maven 项目:

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]

src/main/resources/application.properties [该文件可以为空,但它是强制性的!]
src/main/resources/application.yml [这是你真正的配置文件]