2

Is it possible to load test specific properties when I run integration test. In this case keyspaceApp: test and when ran normally should load keyspaceApp: abc

.yml file

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

Integration 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 ****");
    }
}

Main class

@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);

    }
}

Bean

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

    private Cluster cluster;
    private Session session;

    @Value("${cassandra.keyspaceApp:@null}")
    private String keyspaceApp;
Ilya Ovesnov
  • 4,079
  • 1
  • 27
  • 31
plzdontkillme
  • 1,497
  • 3
  • 20
  • 38
  • Can't this help you http://stackoverflow.com/questions/27390085/spring-boot-read-yml-properties-via-integration-test-case/27396456#27396456 ? Do you have any specific issue? – Artem Bilan Dec 11 '14 at 18:56
  • I am little confused, do I need separate application.yml in the /test/resources or only 1 application.xml at /main/resources having develop and test configs ? Issue is if I have separate .yml files, the key urls which is in /main/resources is not getting substituted as it is not in /test/resources – plzdontkillme Dec 11 '14 at 19:14
  • Two tips: don't use `@PropertySource` (I'm not sure it works with YAML and Boot doesn't need it); and don't put `@Profile` on a test case (I think you mean `@ActiveProfiles`). – Dave Syer Dec 12 '14 at 07:41

1 Answers1

0

The documentation here explains how to do that.

In your case the .yml would look like this :

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

spring:
  profiles: test
cassandra:
  keyspaceApp: test

You could also put your test config in a separate application-test.yml in your test/resources folder, without the spring+profiles lines, just so :

cassandra:
  keyspaceApp: test
Sébastien Nussbaumer
  • 6,202
  • 5
  • 40
  • 58