从集成测试或默认.yml运行时加载测试特定属性

时间:2014-12-11 18:05:51

标签: java spring integration-testing spring-boot

运行集成测试时是否可以加载测试特定属性。在这种情况下,keypaceApp:test和正常运行时应加载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;

1 个答案:

答案 0 :(得分:0)

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