Spring Boot从Controller层进行单元测试的实现
单元测试是程序员对代码的自测,一般公司都会严格要求单元测试,这是对自己代码的负责,也是对代码的敬畏。
一般单元测试都是测试Service层,下面我将演示从Controller层进行单元测试。
无参Controller单元测试示例:
packagecom.pingan.bloan.genesis.controller.base; importorg.junit.After; importorg.junit.Before; importorg.junit.runner.RunWith; importcom.pingan.bloan.genesis.logwrapper.LogWrapper; importorg.springframework.boot.test.context.SpringBootTest; importorg.springframework.test.context.junit4.SpringRunner; importcom.pingan.bloan.genesis.logwrapper.LogWrapperFactory; importorg.springframework.test.context.web.WebAppConfiguration; @RunWith(SpringRunner.class) @SpringBootTest @WebAppConfiguration publicclassBaseSpringBootTest{ protectedLogWrapperlogger=LogWrapperFactory.getLogWrapper(BaseSpringBootTest.class); @Before publicvoidinit(){ logger.info("开始测试..."); } @After publicvoidafter(){ logger.info("测试结束..."); } }
packagecom.pingan.bloan.genesis.controller; importorg.junit.Test; importorg.junit.Before; importorg.springframework.test.web.servlet.MockMvc; importorg.springframework.test.web.servlet.MvcResult; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.test.web.servlet.setup.MockMvcBuilders; importcom.pingan.bloan.genesis.controller.base.BaseSpringBootTest; importorg.springframework.test.web.servlet.result.MockMvcResultHandlers; importorg.springframework.test.web.servlet.result.MockMvcResultMatchers; importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders; /** *DemoController测试 *@author * */ publicclassDemoControllerTestextendsBaseSpringBootTest{ @Autowired privateDemoControllerdemoController; privateMockMvcmockMvc; @Before publicvoidsetup(){ mockMvc=MockMvcBuilders.standaloneSetup(demoController).build(); } @Test publicvoiddemo()throwsException{ MvcResultmvcResult=mockMvc.perform(MockMvcRequestBuilders.get("/demo/demo")) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); logger.info(mvcResult.getResponse().getContentAsString()); } }
有参Controller单元测试示例一:
packagecom.pingan.bloan.genesis.controller; importorg.junit.Test; importorg.junit.Before; importorg.springframework.http.MediaType; importorg.springframework.test.web.servlet.MockMvc; importorg.springframework.test.web.servlet.MvcResult; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.test.web.servlet.setup.MockMvcBuilders; importcom.pingan.bloan.genesis.controller.base.BaseSpringBootTest; importorg.springframework.test.web.servlet.result.MockMvcResultHandlers; importorg.springframework.test.web.servlet.result.MockMvcResultMatchers; importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders; /** *加解密controller测试 * *@author * */ publicclassMessageEncryptionControllerTestextendsBaseSpringBootTest{ @Autowired privateMessageEncryptionControllermessageEncryptionController; privateMockMvcmockMvc; @Before publicvoidsetup(){ mockMvc=MockMvcBuilders.standaloneSetup(messageEncryptionController).build(); } /** *加密测试 *@throwsException */ @Test publicvoidencryption()throwsException{ MvcResultmvcResult=mockMvc.perform(MockMvcRequestBuilders.post("/secret/encryption") .accept(MediaType.APPLICATION_JSON).param("originContent","15221365094")) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); logger.info(mvcResult.getResponse().getContentAsString()); } /** *解密测试 *@throwsException */ @Test publicvoiddeciphering()throwsException{ MvcResultmvcResult=mockMvc.perform(MockMvcRequestBuilders.post("/secret/deciphering") .accept(MediaType.APPLICATION_JSON).param("secretContent","4BD6EE1A13593F97E6CEA20A2BA9E6E4")) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); logger.info(mvcResult.getResponse().getContentAsString()); } }
有参Controller单元测试示例二:
packagecom.pingan.bloan.genesis.controller; importorg.junit.Test; importorg.junit.Before; importcom.alibaba.fastjson.JSONObject; importorg.springframework.http.MediaType; importorg.springframework.test.web.servlet.MockMvc; importorg.springframework.test.web.servlet.MvcResult; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.test.web.servlet.setup.MockMvcBuilders; importcom.pingan.bloan.genesis.controller.base.BaseSpringBootTest; importorg.springframework.test.web.servlet.result.MockMvcResultHandlers; importorg.springframework.test.web.servlet.result.MockMvcResultMatchers; importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders; importcom.pingan.bloan.genesis.model.mobiletelephone.MobileTelephoneRequest; /** *客户手机掩码信息加密处理测试 *@author * */ publicclassMobileTelephoneIdempotentControllerTestextendsBaseSpringBootTest{ @Autowired privateMobileTelephoneIdempotentControllermobileTelephoneIdempotentController; privateMockMvcmockMvc; @Before publicvoidsetup(){ mockMvc=MockMvcBuilders.standaloneSetup(mobileTelephoneIdempotentController).build(); } /** *保存加密处理的客户信息 *@throwsException */ @Test publicvoidsaveCustomerEncryptionMessage()throwsException{ MobileTelephoneRequestrequest=newMobileTelephoneRequest(); request.setCustomerId("RL20180304000099"); request.setMobileTelephone("18883270484"); request.setCcfMobileTelephone("13904108866"); MvcResultmvcResult=mockMvc.perform(MockMvcRequestBuilders.post("/customerEncryption/save") .contentType(MediaType.APPLICATION_JSON) .content(JSONObject.toJSONString(request))) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); logger.info(mvcResult.getResponse().getContentAsString()); } }
Suite一次性执行多个单元测试:
packagecom.pingan.bloan.genesis.controller; importorg.junit.runners.Suite; importorg.junit.runner.RunWith; /** *打包测试 *配置测试类,一次性执行所有配置的测试类 *@author * */ @RunWith(Suite.class) @Suite.SuiteClasses({MessageEncryptionControllerTest.class,DemoControllerTest.class}) publicclassSuiteExecuteTests{ //不用写代码,只需要注解即可(在SuiteClasses中配置测试类) }
代码单元测试,让代码更加健壮。
到此这篇关于SpringBoot从Controller层进行单元测试的实现的文章就介绍到这了,更多相关SpringBootController单元测试内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!