【问题标题】:Load test specific properties when run from integration test or default .yml从集成测试或默认 .yml 运行时加载测试特定属性
【发布时间】:2014-12-11 18:05:51
【问题描述】:

在我运行集成测试时是否可以加载测试特定属性。在这种情况下 keyspaceApp: 测试和正常运行时应该加载 keyspaceApp: abc

.yml 文件

 defaults: &defaults
    cassandra:
      keyspaceApp: abc
    solr:
      keyspaceApp: xyz
test:
    cassandra:
     keyspaceApp: test

集成测试

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = CommonDataApplication.class)
@IntegrationTest
@Profile("test")
public class CassandraClientTest {

    @Autowired
    CassandraClientNew cassandraClientNew;

    @Test
    public void test(){
        cassandraClientNew.getSession();
        System.out.println(" **** done ****");
    }
}

主类

@EnableAutoConfiguration
@ComponentScan
@PropertySource("classpath:application.yml")
public class CommonDataApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = new SpringApplicationBuilder(CommonDataApplication.class)
                .web(false).headless(true).main(CommonDataApplication.class).run(args);

    }
}

豆子

@Component
@ConfigurationProperties(prefix="cassandra")
public class CassandraClientNew {

    private Cluster cluster;
    private Session session;

    @Value("${cassandra.keyspaceApp:@null}")
    private String keyspaceApp;

【问题讨论】:

  • 这不能帮助你stackoverflow.com/questions/27390085/… 吗?您有什么具体问题吗?
  • 我有点困惑,我需要 /test/resources 中的单独 application.yml 还是 /main/resources 中只有 1 个 application.xml 具有开发和测试配置?问题是如果我有单独的 .yml 文件,则 /main/resources 中的关键 url 不会被替换,因为它不在 /test/resources
  • 两个提示:不要使用@PropertySource(我不确定它是否适用于 YAML 并且 Boot 不需要它);并且不要将@Profile 放在测试用例上(我认为您的意思是@ActiveProfiles)。

标签: java spring integration-testing spring-boot


【解决方案1】:

The documentation here 解释了如何做到这一点。

在您的情况下,.yml 看起来像这样:

cassandra:
  keyspaceApp: abc
solr:
  keyspaceApp: xyz
---

spring:
  profiles: test
cassandra:
  keyspaceApp: test

你也可以将你的测试配置放在你的 test/resources 文件夹中的一个单独的 application-test.yml 中,没有 spring+profiles 行,就像这样:

cassandra:
  keyspaceApp: test

【讨论】:

    猜你喜欢
    • 2013-05-29
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    相关资源
    最近更新 更多