【问题标题】:@Value "Could not resolve placeholder" in Spring Boot TestSpring Boot 测试中的@Value“无法解析占位符”
【发布时间】:2016-01-20 07:06:28
【问题描述】:

我想对 Spring-boot 进行 Junit 测试,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ApplicationTest.class})
public class TestOnSpring {
    @Value("${app.name}")
    private String appName;

    @Test
    public void testValue(){
        System.out.println(appName);
    }
}

和这样的ApplicationTest.java

@ComponentScan("org.nerve.jiepu")
@EnableAutoConfiguration()
public class ApplicationTest {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationTest.class, args);
    }
}

我的 POM 是这样的:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.BUILD-SNAPSHOT</version>
    </parent>

当我运行测试时,我得到以下错误信息

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'app.name' in string value "${app.name}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:807)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1027)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543)
    ... 31 more

但是当我将这个应用程序作为普通 Java 应用程序运行时

@SpringBootApplication
public class Application {

    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }
}

效果很好!

它有什么问题?我应该如何使用 Spring-boot 进行 junit 测试? 非常感谢!

【问题讨论】:

  • 您的测试用例运行错误。您正在使用 Spring Boot,然后使用适当的测试方式。而不是ContextConfiguration 使用SpringApplicationConfiguration

标签: java spring junit spring-boot


【解决方案1】:

你需要添加

@PropertySource("classpath:application.properties")

到你的班级,所以它会选择你的正常配置。

如果您需要不同的配置进行测试,您可以添加

@TestPropertySource(locations="classpath:test.properties")

如果不只是将您的配置文件复制粘贴到 test/resources 文件夹,那么启动将从那里选择。

this

【讨论】:

  • 非常感谢您的帮助!我添加了'code'@TestPropertySource(locations="classpath:test.properties")'code',它可以工作。再次感谢!
  • 澄清一下,您需要将“@TestPropertySource...”添加到运行测试的 java 类中。
【解决方案2】:

您可以使用@SpringBootTest,它会自动创建PropertySourcesPlaceholderConfigurer

这在 Spring Boot 文档的测试章节中有描述。

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-configfileapplicationcontextinitializer-test-utility

【讨论】:

    【解决方案3】:

    您已使用@ContextConfiguration(classes = {ApplicationTest.class}) 注释您的测试类。其中ApplicationTest.class 对提到的包进行组件扫描。当您运行测试时,它会尝试从“main”而不是“test”中的资源文件夹中查找配置。如果您在这种特殊情况下使用 @SpringBootTest(classes = {ClassToBeTested.class}) 或仅使用 @SpringBootTest 注释您的类,我认为(不是 100% 肯定)它会创建一个有限的上下文并从测试/资源中获取属性。

    如果您的属性是特定于测试的,您可以将您的属性/yml 文件命名为application-test.propertiesapplication-test.yml。并在您的测试类中使用@ActiveProfiles("test"),以便它始终读取测试特定的属性文件。

    我通常使用适合我的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 2018-06-17
      • 2013-03-18
      • 1970-01-01
      • 2020-02-18
      • 2017-11-16
      • 2020-04-01
      相关资源
      最近更新 更多