Java 按配置文件使用yaml属性的Spring Boot测试
关于这个话题有很多热门话题,但没有一个对我有用 我有一个非常简单的配置类:Java 按配置文件使用yaml属性的Spring Boot测试,java,spring,configuration-files,Java,Spring,Configuration Files,关于这个话题有很多热门话题,但没有一个对我有用 我有一个非常简单的配置类: @Configuration @ConfigurationProperties(prefix = "props") public class TagIncluder { private static final String PARAMETER_NAME = "tags"; private List<String> tags; public TagIncluder() {
@Configuration
@ConfigurationProperties(prefix = "props")
public class TagIncluder {
private static final String PARAMETER_NAME = "tags";
private List<String> tags;
public TagIncluder() {
tags = new ArrayList<>();
}
public List<String> getTags() {
return tags;
}
@Handler
public void attachIncludedTags(Exchange exchange) {
exchange.getIn().setHeader(PARAMETER_NAME, tags);
}
}
最后,测试用例:
@SpringBootTest
@ActiveProfiles("tag_test")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TagIncluder.class)
public class TagIncluderTest extends ExchangeTestSupport {
@Autowired
private TagIncluder sut;
@Test
public void attachIncludedTags_shouldUseTagsInFileIfFileSpecified() {
Exchange testExchange = createExchange();
sut.attachIncludedTags(testExchange);
Assertions.assertThat(testExchange.getIn().getHeader("tags", List.class))
.size().isGreaterThanOrEqualTo(1);
}
}
此外,我还尝试将application.properties
文件放置在上述位置,内容如下:
spring.profiles.active=tag_test
Spring将yaml文件设置为测试中的测试类所需的配置需要什么
更新
因此,经过一些探索和尝试,我发现以下工作:
@SpringBootTest
@ActiveProfiles("tag_test")
@RunWith(SpringJUnit4ClassRunner.class)
public class TagIncluderTest extends ExchangeTestSupport {
@Autowired
private TagIncluder sut;
@Test
public void attachIncludedTags_shouldUseTagsInFileIfFileSpecified() {
Exchange testExchange = createExchange();
sut.attachIncludedTags(testExchange);
Assertions.assertThat(testExchange.getIn().getHeader("tags", List.class))
.size().isGreaterThanOrEqualTo(1);
}
}
这里的不同之处在于,我删除了@ContextConfiguration
注释,并让Spring处理所有这些
这要慢得多,我更愿意指定需要什么。我认为这可能会在将来打破,例如,如果我添加另一个配置类,该类将从整个上下文开始并抛出错误,因为这些属性不包括在我的application-tag_test.yml
配置中
最后,我为配置尝试的上述位置中的任何一个对于上述注释都是有效的。不需要使用application.properties
来指定配置文件
如果有人知道如何指定应该加载到上下文中的内容,我将非常感谢另一种解决方案。Spring Boot测试文档说明
External properties, logging, and other features of Spring Boot are installed in the context by default only if you use SpringApplication to create it.
这意味着您需要有一个工作的Spring引导应用程序,以便在测试用例中测试与属性加载相关的任何内容
此外,从属性设置列表需要一个setter。这项工作:
@Configuration
@ConfigurationProperties(prefix = "props")
public class TagIncluder {
private List<String> tags;
public void setTags(List<String> tags) {
this.tags = tags;
}
public List<String> getTags() {
return tags;
}
}
@Component
public class MyComponent {
@Autowired
TagIncluder tagIncluder;
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@SpringBootTest
@ActiveProfiles("tag_test")
@RunWith(SpringRunner.class)
public class TagIncluderTest {
@Autowired
private TagIncluder sut;
@Test
public void attachIncludedTags_shouldUseTagsInFileIfFileSpecified() {
System.out.println(sut.getTags());
}
}
@配置
@配置属性(前缀=“props”)
公共类标记器{
私有列表标签;
公共无效集合标记(列表标记){
this.tags=标签;
}
公共列表getTags(){
返回标签;
}
}
@组成部分
公共类MyComponent{
@自动连线
TagIncluder TagIncluder;
}
@SpringBoot应用程序
公共类应用程序{
公共静态void main(字符串[]args){
SpringApplication.run(Application.class,args);
}
}
@春靴测试
@ActiveProfiles(“标签测试”)
@RunWith(SpringRunner.class)
公共类TagIncluderTest{
@自动连线
私有标记包括sut;
@试验
public void attachingcludedtags\u应使用tagsinfileiffilespecified(){
System.out.println(sut.getTags());
}
}
弹簧启动测试文档说明
External properties, logging, and other features of Spring Boot are installed in the context by default only if you use SpringApplication to create it.
这意味着您需要有一个工作的Spring引导应用程序,以便在测试用例中测试与属性加载相关的任何内容
此外,从属性设置列表需要一个setter。这项工作:
@Configuration
@ConfigurationProperties(prefix = "props")
public class TagIncluder {
private List<String> tags;
public void setTags(List<String> tags) {
this.tags = tags;
}
public List<String> getTags() {
return tags;
}
}
@Component
public class MyComponent {
@Autowired
TagIncluder tagIncluder;
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@SpringBootTest
@ActiveProfiles("tag_test")
@RunWith(SpringRunner.class)
public class TagIncluderTest {
@Autowired
private TagIncluder sut;
@Test
public void attachIncludedTags_shouldUseTagsInFileIfFileSpecified() {
System.out.println(sut.getTags());
}
}
@配置
@配置属性(前缀=“props”)
公共类标记器{
私有列表标签;
公共无效集合标记(列表标记){
this.tags=标签;
}
公共列表getTags(){
返回标签;
}
}
@组成部分
公共类MyComponent{
@自动连线
TagIncluder TagIncluder;
}
@SpringBoot应用程序
公共类应用程序{
公共静态void main(字符串[]args){
SpringApplication.run(Application.class,args);
}
}
@春靴测试
@ActiveProfiles(“标签测试”)
@RunWith(SpringRunner.class)
公共类TagIncluderTest{
@自动连线
私有标记包括sut;
@试验
public void attachingcludedtags\u应使用tagsinfileiffilespecified(){
System.out.println(sut.getTags());
}
}
在上述Jans建议的指导下,我成功地将测试隔离到一个片段中。自动配置测试是专门写的,但是它只涉及预定义的@..Test
注释
例如,如果深入研究,您将找到@ImportAutoConfiguration
注释
使用它,我们可以告诉测试类为应用程序的单个部分启用自动配置。有教程可供选择。可以在中找到可用于自动配置的工厂的完整列表
最后,这是整个测试类:
@ActiveProfiles("tag_test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TagIncluder.class)
@ImportAutoConfiguration(classes = ConfigurationPropertiesAutoConfiguration.class)
public class TagIncluderTest extends ExchangeTestSupport {
@Autowired
private TagIncluder sut;
@Test
public void attachIncludedTags_shouldUseTagsInFileIfFileSpecified() {
Exchange testExchange = createExchange();
sut.attachIncludedTags(testExchange);
Assertions.assertThat(testExchange.getIn().getHeader("tags", List.class))
.size().isGreaterThanOrEqualTo(1);
}
}
被测试的类未被触及
因此,现在我们可以:
- 使用配置文件
- 使用yaml
- 在Spring上下文中只测试我们想要的类
这非常有启发性:)在上述Jan建议的指导下,我成功地将测试隔离到一个部分。自动配置测试是专门写的,但是它只涉及预定义的
@..Test
注释
例如,如果深入研究,您将找到@ImportAutoConfiguration
注释
使用它,我们可以告诉测试类为应用程序的单个部分启用自动配置。有教程可供选择。可以在中找到可用于自动配置的工厂的完整列表
最后,这是整个测试类:
@ActiveProfiles("tag_test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TagIncluder.class)
@ImportAutoConfiguration(classes = ConfigurationPropertiesAutoConfiguration.class)
public class TagIncluderTest extends ExchangeTestSupport {
@Autowired
private TagIncluder sut;
@Test
public void attachIncludedTags_shouldUseTagsInFileIfFileSpecified() {
Exchange testExchange = createExchange();
sut.attachIncludedTags(testExchange);
Assertions.assertThat(testExchange.getIn().getHeader("tags", List.class))
.size().isGreaterThanOrEqualTo(1);
}
}
被测试的类未被触及
因此,现在我们可以:
- 使用配置文件
- 使用yaml
- 在Spring上下文中只测试我们想要的类
这非常有启发性:)您尝试过将´-Dspring.profiles.active=tag_test`作为JRE参数吗?刚刚尝试过,不幸的是,同样的结果。感谢您的建议您是否尝试过将´-Dspring.profiles.active=tag_test`作为JRE参数?刚刚尝试过,不幸的是,同样的结果。谢谢你的建议非常感谢你分享这个@ImportAtoConfiguration(classes=ConfigurationPropertiesAutoConfiguration.class)是我在尝试构建我们服务的部分应用程序上下文时真正缺少的一部分。非常感谢您分享这一点@ImportAutoConfiguration(classes=ConfigurationPropertiesAutoConfiguration.class)确实是我在尝试构建服务的部分应用程序上下文时缺少的一部分。