plzdontkillme
plzdontkillme

Reputation: 1527

Spring boot, read yml properties via integration test case

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.

Problem is value of urls and keyspaceApp is null

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

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

Upvotes: 10

Views: 25083

Answers (5)

Ahmed Hassanien
Ahmed Hassanien

Reputation: 537

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 {
}

Upvotes: 4

Abhilekh Singh
Abhilekh Singh

Reputation: 2963

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>.

Upvotes: 7

Juha Hanka
Juha Hanka

Reputation: 659

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]

Upvotes: 0

okrunner
okrunner

Reputation: 3193

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

Upvotes: 13

Artem Bilan
Artem Bilan

Reputation: 121542

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.

Upvotes: 8

Related Questions