簡體   English   中英

Spring 啟動測試中的@Value“無法解析占位符”

[英]@Value "Could not resolve placeholder" in Spring Boot Test

我想對 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考試? 多謝!

你需要添加

@PropertySource("classpath:application.properties")

到你的班級,所以它會選擇你的正常配置。

如果您需要不同的配置進行測試,您可以添加

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

如果不只是將您的配置文件復制粘貼到test/resources文件夾,則啟動將從那里選擇。

看到這個

您可以使用將自動創建PropertySourcesPlaceholderConfigurer@SpringBootTest

這在 Spring Boot 文檔的測試章節中有描述。

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

您已使用@ContextConfiguration(classes = {ApplicationTest.class})注釋了您的測試類。 其中ApplicationTest.class對提到的包進行組件掃描。 當您運行測試時,它會嘗試從“main”而不是“test”的資源文件夾中查找配置。 如果你的注釋與類@SpringBootTest(classes = {ClassToBeTested.class})或只是@SpringBootTest在這種特殊情況下,我覺得(不是100%確定),它會創建一個有限的范圍內,並從測試/資源拿起屬性。

如果您的屬性是特定於測試的,您可以將您的 properties/yml 文件命名為application-test.propertiesapplication-test.yml 並在您的測試類中使用@ActiveProfiles("test")以便它始終讀取測試特定的屬性文件。

我通常使用這個對我有用的解決方案。

對我來說,問題與應用程序屬性的配置文件集有關。

application.yml

spring:
  config:
    activate:
      on-profile: local
mongodb:
  uri: mongodb+srv:/

Class 參考值:

@Value("${mongodb.uri}")
private String uri;

測試 class 方法中的解析:

@SpringBootTest
@ActiveProfiles("local")
class myTestClass {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM