使用Spring配置文件读取外部YML文件的测试失败。

3
我正在使用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文件,正如在这篇文章Spring Boot中使用YAML文件的@PropertySource中所述:

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

问题在于我无法找到适当的“切片”来隔离测试配置属性。事实上,以下测试将失败:
@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");
    }

}

如何解决这个问题?我是否可以向slice中添加某种初始化程序或类似的东西来使测试成功? 在这里,您可以找到整个GitHub项目。

我用两种不同的方法解决了一个类似的问题:https://dev59.com/1sH5oIgBc1ULPQZFUQ8B#73110842 - Paul Marcelin Bejan
1个回答

0
将@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可能具有非常复杂的结构来加载。 - riccardo.cardin
是的,这很奇怪,@ConfigurationProperties应该可以工作,但它却没有。我尝试添加@EnableConfigurationProperties,但它仍然不起作用。 - Ben Miao
我知道。我建议删除答案,并在你找到解决方案后发布新的答案 :) - riccardo.cardin
看看这个,它可能会解决你的问题https://dev59.com/ErTma4cB1Zd3GeqPzgEB - Ben Miao
你分享的SO问题如何解决我的问题?它甚至都不相关 :| - riccardo.cardin
@riccardo.cardin,我找到了解决这个问题的方法,请再次查看我的答案 :) - Ben Miao

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接