【问题标题】:Test on Spring configuration properties reading an external YML file doesn't work测试读取外部 YML 文件的 Spring 配置属性不起作用
【发布时间】:2021-09-23 05:37:19
【问题描述】:

我正在使用 Spring Boot 2.4.8,并且正在将读取自外部 YML 文件的信息读入 bean:

@Component
@ConfigurationProperties(prefix = "my.conf")
@PropertySource(value = "classpath:ext.yml", factory = YamlPropertySourceFactory.class)
public class MyExternalConfProp {
  private String property;
  
  public void setProperty(String property) {
    this.property = property;
  }

  public String getProperty() {
    return property;
  }
}

我定义了一个自定义工厂来读取外部 YML 文件,如文章@PropertySource with YAML Files in Spring Boot 中所述:

public class YamlPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(encodedResource.getResource());

        Properties properties = factory.getObject();

        return new PropertiesPropertySource(
            Objects.requireNonNull(encodedResource.getResource().getFilename()),
            Objects.requireNonNull(properties));
    }

}

YML文件内容如下:

my.conf.property: yeyeye

问题是我找不到合适的 slice 来单独测试配置属性。事实上,下面的测试失败了:

@SpringBootTest(classes = {MyExternalConfProp.class})
class MyExternalConfPropTest {

  @Autowired
  private MyExternalConfProp confProp;

  @Test
  void externalConfigurationPropertyShouldBeLoadedIntoSpringContext() {
    assertThat(confProp).hasFieldOrPropertyWithValue("property", "yeyeye");
  }
}

正如我们所说,测试失败并显示以下消息:

java.lang.AssertionError: 
Expecting
  <in.rcard.externalconfprop.MyExternalConfProp@4cb40e3b>
to have a property or a field named <"property"> with value
  <"yeyeye">
but value was:
  <null>

然而,如果我不使用任何切片,则测试成功:

@SpringBootTest
class ExternalConfPropApplicationTests {

    @Autowired
    private MyExternalConfProp confProp;

    @Test
    void contextLoads() {
        assertThat(confProp).hasFieldOrPropertyWithValue("property", "yeyeye");
    }

}

我该如何解决这个问题?是否可以将一些 initializer 或类似的东西添加到 slice 以使测试成功?

Here你可以在 GitHub 上找到整个项目。

【问题讨论】:

    标签: java spring-boot spring-boot-test configurationproperties


    【解决方案1】:

    将@EnableConfigurationProperties 添加到您的测试或在您的测试上启动spring boot 应用程序将解决问题

    @EnableConfigurationProperties
    @SpringBootTest(classes = {MyExternalConfProp.class})
    class MyExternalConfPropTest {
    
      @Autowired
      private MyExternalConfProp confProp;
    
      @Test
      void externalConfigurationPropertyShouldBeLoadedIntoSpringContext() {
        assertThat(confProp).hasFieldOrPropertyWithValue("property", "yeyeye");
      }
    }
    

    @SpringBootTest(classes = {YourSpringBootApplication.class})
    class MyExternalConfPropTest {
    
      @Autowired
      private MyExternalConfProp confProp;
    
      @Test
      void externalConfigurationPropertyShouldBeLoadedIntoSpringContext() {
        assertThat(confProp).hasFieldOrPropertyWithValue("property", "yeyeye");
      }
    }
    

    【讨论】:

    • 我正在使用@ConfigurationProperties,因为我不想使用@Value 注释:) 读取的bean 可能需要加载一个非常复杂的结构。
    • 是的,这很奇怪,@ConfigurationProperties 应该可以工作,但不行,我尝试添加 @EnableConfigurationProperties,但仍然不行
    • 我知道。我建议删除答案,并在您有解决方案时发布新答案:)
    • 看看这个,它可能会解决你的问题stackoverflow.com/questions/56234234/…
    • 您分享的 SO 问题应该如何解决我的问题?它甚至不相关:|
    猜你喜欢
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 2023-01-30
    • 2017-08-18
    • 2017-07-12
    • 2021-05-30
    相关资源
    最近更新 更多