博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBootTest自动注入
阅读量:6882 次
发布时间:2019-06-27

本文共 2622 字,大约阅读时间需要 8 分钟。

hot3.png

not work on 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

@SpringBootApplicationpublic 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!

 

Answer:

You can use the @SpringBootTest that will do create the PropertySourcesPlaceholderConfigurer automatically.

This is described in the Testing chapter of the

转载于:https://my.oschina.net/u/2935389/blog/907589

你可能感兴趣的文章
怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
查看>>
Tensorflow学习笔记(1):tf.slice()函数使用
查看>>
ORA-01102的解决办法
查看>>
奇怪的iphone6 plus,H5调用拍照浏览器崩溃
查看>>
MVC接受JSON的一些注意事项
查看>>
response对象设置输出缓冲大小
查看>>
MVC+Ninject+三层架构+代码生成 -- 总结(七、顯示層 一)
查看>>
[CF1105D]Kilani and the Game
查看>>
[bzoj4195][Noi2015]程序自动分析
查看>>
简单的bfs(最短路径)c++
查看>>
Matlab2013a许可证过期问题,反复提示激活
查看>>
向上下左右不间断无缝滚动图片的效果(兼容火狐和IE)
查看>>
MongoDB 基础
查看>>
redis分布式集群3种架构方案
查看>>
C++ 编程思想——继承和组合
查看>>
Charles抓包显示乱码解决方法
查看>>
Web前端开发中的MCRV模式(转)
查看>>
VC中的字符串转换宏
查看>>
SVN过滤设置 ...
查看>>
POJ 3185 DFS
查看>>