Spring Boot從Controller層進(jìn)行單元測試的實(shí)現(xiàn)
單元測試是程序員對代碼的自測,一般公司都會嚴(yán)格要求單元測試,這是對自己代碼的負(fù)責(zé),也是對代碼的敬畏。
一般單元測試都是測試Service層,下面我將演示從Controller層進(jìn)行單元測試。
無參Controller單元測試示例:
package com.pingan.bloan.genesis.controller.base; import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; import com.pingan.bloan.genesis.logwrapper.LogWrapper; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.pingan.bloan.genesis.logwrapper.LogWrapperFactory; import org.springframework.test.context.web.WebAppConfiguration; @RunWith(SpringRunner.class) @SpringBootTest @WebAppConfiguration public class BaseSpringBootTest { protected LogWrapper logger = LogWrapperFactory.getLogWrapper(BaseSpringBootTest.class); @Before public void init() { logger.info("開始測試..."); } @After public void after() { logger.info("測試結(jié)束..."); } }
package com.pingan.bloan.genesis.controller; import org.junit.Test; import org.junit.Before; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import com.pingan.bloan.genesis.controller.base.BaseSpringBootTest; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; /** * DemoController測試 * @author * */ public class DemoControllerTest extends BaseSpringBootTest { @Autowired private DemoController demoController; private MockMvc mockMvc; @Before public void setup() { mockMvc = MockMvcBuilders.standaloneSetup(demoController).build(); } @Test public void demo() throws Exception { MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/demo/demo")) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); logger.info(mvcResult.getResponse().getContentAsString()); } }
有參Controller單元測試示例一:
package com.pingan.bloan.genesis.controller; import org.junit.Test; import org.junit.Before; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import com.pingan.bloan.genesis.controller.base.BaseSpringBootTest; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; /** * 加解密controller測試 * * @author * */ public class MessageEncryptionControllerTest extends BaseSpringBootTest { @Autowired private MessageEncryptionController messageEncryptionController; private MockMvc mockMvc; @Before public void setup() { mockMvc = MockMvcBuilders.standaloneSetup(messageEncryptionController).build(); } /** * 加密測試 * @throws Exception */ @Test public void encryption() throws Exception { MvcResult mvcResult = 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()); } /** * 解密測試 * @throws Exception */ @Test public void deciphering() throws Exception { MvcResult mvcResult = 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單元測試示例二:
package com.pingan.bloan.genesis.controller; import org.junit.Test; import org.junit.Before; import com.alibaba.fastjson.JSONObject; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import com.pingan.bloan.genesis.controller.base.BaseSpringBootTest; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import com.pingan.bloan.genesis.model.mobiletelephone.MobileTelephoneRequest; /** * 客戶手機(jī)掩碼信息加密處理測試 * @author * */ public class MobileTelephoneIdempotentControllerTest extends BaseSpringBootTest { @Autowired private MobileTelephoneIdempotentController mobileTelephoneIdempotentController; private MockMvc mockMvc; @Before public void setup() { mockMvc = MockMvcBuilders.standaloneSetup(mobileTelephoneIdempotentController).build(); } /** * 保存加密處理的客戶信息 * @throws Exception */ @Test public void saveCustomerEncryptionMessage() throws Exception { MobileTelephoneRequest request = new MobileTelephoneRequest(); request.setCustomerId("RL20180304000099"); request.setMobileTelephone("18883270484"); request.setCcfMobileTelephone("13904108866"); MvcResult mvcResult = 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一次性執(zhí)行多個(gè)單元測試:
package com.pingan.bloan.genesis.controller; import org.junit.runners.Suite; import org.junit.runner.RunWith; /** * 打包測試 * 配置測試類,一次性執(zhí)行所有配置的測試類 * @author * */ @RunWith(Suite.class) @Suite.SuiteClasses({MessageEncryptionControllerTest.class, DemoControllerTest.class}) public class SuiteExecuteTests { // 不用寫代碼,只需要注解即可(在SuiteClasses中配置測試類) }
代碼單元測試,讓代碼更加健壯。
到此這篇關(guān)于Spring Boot從Controller層進(jìn)行單元測試的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Spring Boot Controller單元測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成P6Spy實(shí)現(xiàn)SQL日志的記錄詳解
P6Spy是一個(gè)框架,它可以無縫地?cái)r截和記錄數(shù)據(jù)庫活動,而無需更改現(xiàn)有應(yīng)用程序的代碼。一般我們使用的比較多的是使用p6spy打印我們最后執(zhí)行的sql語句2022-11-11Java中使用開源庫JSoup解析HTML文件實(shí)例
這篇文章主要介紹了Java中使用開源庫JSoup解析HTML文件實(shí)例,Jsoup是一個(gè)開源的Java庫,它可以用于處理實(shí)際應(yīng)用中的HTML,比如常見的HTML格式化就可以用它來實(shí)現(xiàn),需要的朋友可以參考下2014-09-09Spring與Shiro整合及加載權(quán)限表達(dá)式問題
這篇文章主要介紹了Spring與Shiro整合及加載權(quán)限表達(dá)式問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車剖析
這篇文章主要為大家介紹了java并發(fā)使用CountDownLatch在生產(chǎn)環(huán)境翻車的示例剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08