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
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")
...
}
}
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.
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>.
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 {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With