【问题标题】:Spring boot tests - Can't find test properties春季启动测试 - 找不到测试属性
【发布时间】:2018-01-21 09:28:39
【问题描述】:

我有一个 Spring Boot 项目,效果很好。我现在想为我的应用程序编写测试,但我遇到了一些配置问题。

Spring boot 为我创建了一个名为 ApplicationTests 的测试类。它真的很简单,看起来像这样:

@RunWith(SpringRunner.class)
@SpringBootTest
public class DuurzaamApplicationTests {
    @Test
    public void contextLoads() {
    }    
}

现在当我开始测试时,我得到了这个错误:

java.lang.IllegalArgumentException: Could not resolve placeholder 'company.upload' in value "${company.upload}"

我在 src/test/resources 目录中有一个 properties.yml 文件,但由于某种原因它没有加载。我已经从互联网上的示例中尝试了所有不同类型的注释,但它们都不起作用。

如何告诉 Spring Boot 测试使用 application.yml 文件来加载属性?

【问题讨论】:

标签: spring unit-testing spring-boot


【解决方案1】:

我们可以使用@TestPropertySource或@PropertySource来加载属性文件。

例子:

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource("classpath:properties.yml")
@ActiveProfiles("test")
public class DuurzaamApplicationTests {
    @Test
    public void contextLoads() {
    }    
}

文档:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/TestPropertySource.html

【讨论】:

  • 不起作用。我仍然收到“无法解析值“${company.upload}”中的占位符 'company.upload'。我的 application.yml 位于 src/test/resources 目录中。
  • 你能在 gist 或 git 中分享项目吗?
  • 最后一个技巧不幸地帮助解决了属性值,然后 spring 数据源没有被覆盖。 application-test.yml 解析属性值,但仍在使用 spring 数据源,而不是 application-test.yml 中提供的 h2 数据库。我尝试使用这两个应用程序文件,也使用@TestPropertySource 并指示两个应用程序文件,然后值再次未解析。我猜春天的到来会有些奇怪。
  • 使用@ActiveProfiles("test") 并尝试
  • 在下面查看我的答案。不支持.yml,仅支持.properties 和.xml。
【解决方案2】:

令我惊讶的是,当您在 Spring Boot Test 中加载属性文件时,不支持 .yml。尽管隐含地在文档中注明。

从上面的链接:

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/TestPropertySource.html

支持的文件格式

支持传统和基于 XML 的属性文件格式,例如“classpath:/com/example/test.properties”或“file:/path/to/file.xml”。

.yml 没有被提及。

并且,在将我的.yml 更改为.properties 并重写xx.xx.xx=value 形式的值后,可以正确读取键值对。

好奇怪。

编辑:

现在我找到了一个解决这个问题的票;似乎是 Spring 中一个众所周知的错误。

https://github.com/spring-projects/spring-framework/issues/18486

【讨论】:

  • 如果你不想重写你的.yml,你也可以使用下面的测试:@SpringBootTest( properties = { "my.property=test" }注意属性的格式必须是.properties文件之一,即使用= 作为分隔符。 )`
【解决方案3】:

对我来说,上述解决方案不起作用,并且任何环境变量仍在覆盖@TestPropertySource 中定义的测试属性,即使https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html 表明此源应具有比环境变量更高的优先级。唯一对我有用的解决方案是在测试配置类中手动定义 PropertyPlaceholderConfigurer bean 并将其设置为最高优先级。

这是 Spring Boot 1.5.15.RELEASE

@Configuration
@TestPropertySource(properties = "/application-test.properties")
@Slf4j
public class IntegrationTestConfiguration {

@Bean
public static PropertyPlaceholderConfigurer properties() {
    PropertyPlaceholderConfigurer ppc
          = new PropertyPlaceholderConfigurer();
    Resource[] resources = new ClassPathResource[]
          { new ClassPathResource( "/application-test.properties" ) };
    ppc.setLocations( resources );
    ppc.setIgnoreUnresolvablePlaceholders( true );
    ppc.setOrder( Ordered.HIGHEST_PRECEDENCE );

    return ppc;
}

/// ....

@RunWith( SpringRunner.class )
@ActiveProfiles( "test" )
@Import( IntegrationTestConfiguration.class )
@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT )
public class MyTest {

【讨论】:

    【解决方案4】:

    @PropertySource 和 @TestPropertySource 不适用于 YAML。见this。

    我自己也测试过。尝试创建 2 个文件 - *.yml 和 *.properties 并亲自查看。

    为了使*.yml 工作大多数人使用@SpringBootTest,但如果这不是您想要的并且您想使用@ContextConfiguration,那么您会感到有点惊讶。

    【讨论】:

      【解决方案5】:

      我收到了同样的错误消息,我的问题是 src\test\resources 中的 application.properties 缺少新属性

      【讨论】:

        【解决方案6】:

        有时无法找到您的 application-test.properties 文件,因为它位于类路径之外的子文件夹中。

        比如这个可能找不到,因为文件实际上不在类路径中。

        @TestPropertySource("classpath:application-test.properties")
        

        但如果文件位于类路径中路径之外的 config 文件夹中,则会找到此文件

        @TestPropertySource("classpath:config/application-test.properties")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-13
          • 2017-09-25
          • 2019-03-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多