关闭
Hit
enter
to search or
ESC
to close
May I Suggest ?
#leanote #leanote blog #code #hello world
Okeeper's Blog
Home
Archives
Tags
DevOps
软件笔记
Spring
学习
JVM系列
关于我
使用spring boot +Junit 测试 spring mvc 配置中心项目
无
718
0
0
zhangyue
**首先web工程一般的启动方式是web容器,如tomcat,jetty。然后由于使用了spring的配置中心,你需要指定几个jvm options参数来指定配置中心的git地址以及分支等,但是为了方便测试,为了不用每次测试都启用tomcat,于是想到使用Junit测试用例于是想到普通web项目的测试用例的写法如下:** ```java @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations = {"classpath:spring-servlet.xml","classpath:spring-common.xml"}) public class SpringWebJunitTest { @Autowired private IItemsService itemsService; @Test public void test() { itemsService.queryItemsByClasscode("ddd"); System.out.println("hello world"); } } ``` **然后在在classpath下面加入bootstrap.properties内容如下:** ``` spring.cloud.config.uri=http://localhost:9010 #指定git仓库地址 spring.cloud.config.name=database,business,component #指定配置文件的名称 spring.cloud.config.profile=dev #指定profile的类型,test,dev,prod等 spring.cloud.config.label=dev #指定git的分支 ``` **但是报dataSource的数据源初始化失败(数据库链接配置在spring的配置中心)** ```log 2017-01-11 17:50:29 [INFO] [org.springframework.beans.factory.xml.XmlBeanDefinitionReader:317] - Loading XML bean definitions from class path resource [spring-common.xml] 2017-01-11 17:50:29 [INFO] [org.springframework.context.support.AbstractApplicationContext:578] - Refreshing org.springframework.web.context.support.GenericWebApplicationContext@63676602: startup date [Wed Jan 11 17:50:29 GMT+08:00 2017]; root of context hierarchy [17:50:30|INFO |(com.alibaba.druid.pool.DruidDataSource)]=[{dataSource-1} inited] ``` **从日志可以看出这是因为初始化`sprint-common.xml`失败,然后想到是因为加载不到spring配置中心** **然后想是因为我的bootstrap.properties不起作用吗,试试用jvm options 制定参数呢** 为测试用例添加jvm options: ``` -Dspring.cloud.config.uri=http://localhost:9010 #指定git仓库地址 -Dspring.cloud.config.name=database,business,component #指定配置文件的名称 -Dspring.cloud.config.profile=dev #指定profile的类型,test,dev,prod等 -Dspring.cloud.config.label=dev #指定git的分支 ``` *如图:*  **结果报了一模一样的错误,这是意识到是这两种方式得知配置中心的方法是无效的,于是想想肯定有人和我一样遇到类似的坑吧,结果google了半天无果,无奈啊~** **起身上了个厕所回来一想,spring boot 方式不是能正常加载到spring的配置中心吗,用junit也能正常跑起来,为什么换成web酒跑步起来呢。为了达到测试用例效果,摆了 就用junit 结合spring boot来启用web项目并加载配置中心吧,抱着还不行就不管了的心态试了试。以下是配置过程。** 在项目pom.xml中加入spring boot 启动web项目的支持 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <scope>test</scope> </dependency> ``` 1. 新建Spring boot启动类Application类 ```java @Configuration @EnableAutoConfiguration @ComponentScan @ImportResource({"classpath:spring-servlet.xml","classpath:spring-common.xml"}) public class SprintBootTestApplication extends WebMvcConfigurerAdapter { public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(SprintBootTestApplication.class); System.out.println("spring boot start success."); } } ``` 2. 新建junit测试类型来运行Spring boot 启动类 ``` @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SprintBootTestApplication.class) @WebAppConfiguration public class BaseTest { @Resource(name="testService") public TestService testService; @Test public void test(){ testService.sayHello("world"); System.out.println("finish!!!"); } } ``` **测试运行,果然可以。不知道为什么单纯的使用测试用例不能使用spring配置中心配置,如果哪位大侠有更好的方法,能够分享下让我学习学习啊**
觉得不错,点个赞?
Please enable JavaScript to view the
comments powered by Disqus.
comments powered by
Disqus
文章目录