java value占位符,@Value“无法解析占位符”在Spring Boot Test中

I want to take a Junit test for Spring-boot as below:

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes = {ApplicationTest.class})

public class TestOnSpring {

@Value("${app.name}")

private String appName;

@Test

public void testValue(){

System.out.println(appName);

}

}

and ApplicationTest.java like this

@ComponentScan("org.nerve.jiepu")

@EnableAutoConfiguration()

public class ApplicationTest {

public static void main(String[] args) {

SpringApplication.run(ApplicationTest.class, args);

}

}

and my POM like this:

org.springframework.boot

spring-boot-starter-parent

1.3.0.BUILD-SNAPSHOT

When I run the test, I got below error information

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

But When I run this application as normal Java Application

@SpringBootApplication

public class Application {

public static void main(String[] args){

SpringApplication.run(Application.class, args);

}

}

It work well!

What's wrong with it ? How should I take the junit test with Spring-boot?

Thanks a lot!

解决方案

You need to add

@PropertySource("classpath:application.properties")

to your class, so it will pick your normal configurations.

If you need different configurations for test you can add

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

If not just copy paste your config file to test/resources folder, then boot will pick from there.

See this.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值