您好我正在使用Spring Boot,我想在Bean中注入.yml文件的值。我已经编写了集成测试用例,但看起来像通过Integration测试用例它没有注入值。
问题是url和keyspaceApp的值为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;
}
}
集成测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestBean.class)
@IntegrationTest
public class CassandraClientTest {
@Autowired
private TestBean bean;
@Test
public void test() {
bean.print();
}
}
应用程序yml文件
cassandra:
urls: lllaaa.com
keyspaceApp: customer
createDevKeyspace: true
答案 0 :(得分:10)
以下是我如何使用它:
@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")
...
}
}
答案 1 :(得分:8)
试试这个:
@SpringApplicationConfiguration(classes = TestBean.class, initializers = ConfigFileApplicationContextInitializer.class)
来自JavaDocs:
* {@link ApplicationContextInitializer} that can be used with the
* {@link ContextConfiguration#initializers()} to trigger loading of
* {@literal application.properties}.
它说它适用于application.properties
,但我想它也可以与application.yml
一起使用。
答案 2 :(得分:4)
如果你有' application-test.yml'在资源文件夹中。
你可以试试这个:
import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles("test")
来自它的Java文档:
* {@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>.
答案 3 :(得分:3)
SpringApplicationConfiguration
在spring [Spring Boot v1.4.x]中已弃用,并在以下位置删除:[Spring Boot v1.5.x]。所以,这是一个更新的答案。
MyTestClass
类就像:
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
类就像:
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; }
}
我的application.yml
就像:
my:
property: Hello
最后MyConfiguration
真的是空的:-)你可以用你想要的东西填充它:
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(value = MyYmlProperties.class)
public class MyConfiguration {
}
答案 4 :(得分:0)
这是另一种方式:[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();
}
}
仅当'application.properties'文件存在时,此方法才有效。
<强> e.g。 maven项目:
src / main / resources / application.properties [该文件可以为空,但必须是!]
src / main / resources / application.yml [这是你的真实配置文件]