springboot 系列教程十:springboot单元测试
生活随笔
收集整理的這篇文章主要介紹了
springboot 系列教程十:springboot单元测试
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
單元測試
springboot 支持多種方式的單元測試,方便開發者測試代碼,首先需要在 pom 文件中添加 starter
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency>接著開發一個測試類就可以其實
@RunWith(SpringRunner.class) @SpringBootTest public class TestApplication {@AutowiredUserDao dao;@Testpublic void test(){System.err.println(dao.findAll());} }測試結果如下圖:
由此可見,我們也是可以在測試類里面注入 service、dao 等組件
除了這些常規的測試,springboot 也提供了 MockMvc,可以用來方便的測試 controller 層,我 controller 層代碼如下:
@RestController public class UserController {@AutowiredUserDao dao;//查詢數據,并且返回給頁面顯示@RequestMapping("/index")public String index() {System.out.println(dao.findAll());return "hello world";}@RequestMapping("/getUser")public String getUser(String id, String name) {System.out.println("傳入的參數id為:" + id + "---" + name);return "ok";} }接下來我們寫一個測試類:
@RunWith(SpringRunner.class) @SpringBootTest public class TestController {@AutowiredWebApplicationContext webApplicationContext;private MockMvc mvc;@Beforepublic void init(){System.out.println(111);mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();}/*** 1、mockMvc.perform執行一個請求。* 2、MockMvcRequestBuilders.get("/XXX")構造一個請求。* 3、ResultActions.param添加請求傳值* 4、ResultActions.accept(MediaType.APPLICATION_JSON)設置返回類型* 5、ResultActions.andExpect添加執行完成后的斷言。* 6、ResultActions.andDo添加一個結果處理器,表示要對結果做點什么事情* 比如此處使用MockMvcResultHandlers.print()輸出整個響應結果信息。* 5、ResultActions.andReturn表示執行完成后返回相應的結果。*/@Testpublic void test1() throws Exception{mvc.perform(MockMvcRequestBuilders.get("/index").accept(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn();}//帶參數測試@Testpublic void test2() throws Exception{mvc.perform(MockMvcRequestBuilders.get("/getUser").param("id","1001","admin").accept(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn();} }測試結果
完整結果:
MockHttpServletRequest:HTTP Method = GETRequest URI = /getUserParameters = {id=[1001, admin]}Headers = [Accept:"application/json"]Body = <no character encoding set>Session Attrs = {}Handler:Type = com.bdqn.zmj.controller.UserControllerMethod = public java.lang.String com.bdqn.zmj.controller.UserController.getUser(java.lang.String,java.lang.String)Async:Async started = falseAsync result = nullResolved Exception:Type = nullModelAndView:View name = nullView = nullModel = nullFlashMap:Attributes = nullMockHttpServletResponse:Status = 200Error message = nullHeaders = [Content-Type:"application/json;charset=UTF-8", Content-Length:"2"]Content type = application/json;charset=UTF-8Body = okForwarded URL = nullRedirected URL = nullCookies = []測試類里面的這些測試代碼,我相信是個寫過基礎 web 的人員都能看懂吧!!!!
轉載于:https://my.oschina.net/zhoumj/blog/3040139
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的springboot 系列教程十:springboot单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dnf云珂在什么位置 最强自定义装备是什
- 下一篇: 拉结尔怎么激活英雄