春季启动可以和大部分流行的测试框架协同工作:通过春天JUnit创建单元测试;生成测试数据初始化数据库用于测试;弹簧引导可以跟BDD(行为主要驱动开发)工具,黄瓜和斯波克协同工作,对应用程序进行测试。
进行软件开发的时候,我们会写很多代码,不过,再过六个月(甚至一年以上)你知道自己的代码怎么运作么?通过测试(单元测试,集成测试,接口测试)可以保证系统的可维护性,当我们修改了某些代码时,通过回归测试可以检查是否引入了新的错误。总得来说,测试让系统不再是一个黑盒子,让开发人员确认系统可用。
在网络应用程序中,对控制器层的测试一般有两种方法:(1)发送http请求;(2)模拟http请求对象。第一种方法需要配置回归环境,通过修改代码统计的策略来计算覆盖率;第二种方法是比较正规的思路,但是在我目前经历过的项目中用得不多,今天总结下如何用模拟对象测试控制器层的代码。
在之前的几篇文章中,我们都使用bookpub这个应用程序作为例子,今天也不例外,准备测试它提供的RESTful接口是否能返回正确的响应数据。这种测试不同于单元测试,需要为之初始化完整的应用程序上下文,所有的spring bean都织入以及数据库中需要有测试数据,一般来说这种测试称之为集成测试或者接口测试。
通过spirng。io新建的弹簧启动项目提供了一个空的测试文件,BookPubApplicationTest.java内容是:
@RunWith (SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(类=BookPubApplication.class) 公开课BookPubApplicationTests { @Test 公共空间contextLoads () { } } >之前在pom文件中增加spring-boot-starter-test依赖,添加jsonPath依赖
& lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-test & lt; scope> test & lt;/dependency> & lt; dependency> & lt; groupId> com.jayway.jsonpath & lt; artifactId> json-path & lt;/dependency> >之前在BookPubApplicationTest中添加测试用例
包com.test.bookpub; 进口com.test.bookpub.domain.Book; 进口com.test.bookpub.repository.BookRepository; 进口org.junit.Before;进口org.junit.Test; 进口org.junit.runner.RunWith; 进口org.springframework.beans.factory.annotation.Autowired; 进口org.springframework.beans.factory.annotation.Value; 进口org.springframework.boot.test.SpringApplicationConfiguration; 进口org.springframework.boot.test.TestRestTemplate; 进口org.springframework.boot.test.WebIntegrationTest; 进口org.springframework.http.MediaType; 进口org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 进口org.springframework.test.web.servlet.MockMvc; 进口org.springframework.test.web.servlet.setup.MockMvcBuilders; 进口org.springframework.web.client.RestTemplate; 进口org.springframework.web.context.WebApplicationContext; 进口静态org.junit.Assert.assertEquals; 进口静态org.junit.Assert.assertNotNull; 进口静态org.hamcrest.Matchers.containsString; 进口静态org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 进口静态org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 进口静态org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; 进口静态org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith (SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(类=BookPubApplication.class) @WebIntegrationTest (server.port: 0) 公开课BookPubApplicationTests { @ autowired 私人WebApplicationContext上下文; @ autowired 私人BookRepository BookRepository; @ value (" $ {local.server.port} ") 私人int端口; 私人MockMvc MockMvc; 私人创建RestTemplate创建RestTemplate=new TestRestTemplate (); @Before 公共空间setupMockMvc () { mockMvc=MockMvcBuilders.webAppContextSetup(上下文).build (); } @Test 公共空间contextLoads () { assertequal (bookRepository.count ()); } @Test 公共空间webappBookIsbnApi () { 书书=创建restTemplate。getForObject (“http://localhost”+港口+“/书/9876-5432-1111”,Book.class); assertNotNull(书); assertequal(“中文测试”,book.getPublisher () . getname ()); } @Test 公共空间webappPublisherApi()抛出异常{//MockHttpServletRequestBuilder.accept方法是设置客户端可识别的内容类型//MockHttpServletRequestBuilder.contentType,设置请求头中的内容类型字段,表示请求体的内容类型 mockMvc.perform (get("/出版商/1 ") .accept (MediaType.APPLICATION_JSON_UTF8)) .isOk .andExpect(状态()()) .andExpect(内容().string (containsString(“中文测试”))) .andExpect (jsonPath ($ . name) value(“中文测试")); } }SpringBoot项目中使用5的示例代码