Flask接口签名sign原理与实例代码浅析
680
2022-12-08
Spring Boot从Controller层进行单元测试的实现
单元测试是程序员对代码的自测,一般公司都会严格要求单元测试,这是对自己代码的负责,也是对代码的敬畏。
一般单元测试都是测试Service层,下面我将演示从Controller层进行单元测试。
无参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("测试结束...");
}
}
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.bashttp://e.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;
/**
* 客户手机掩码信息加密处理测试
* @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("13ZJwVCgCK904108866");
MvcResulhttp://t 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一次性执行多个单元测试:
package com.pingan.bloan.genesis.controller;
import org.junit.runners.Suite;
import org.junit.runner.RunWith;
/**
* 打包测试
* 配置测试类,一次性执行所有配置的测试类
* @author
*
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({MessageEncryptionControllerTest.class, DemoControllerTest.class})
public class SuiteExecuteTests {
// 不用写代码,只需要注解即可(在SuiteClasses中配置测试类)
}
代码单元测试,让代码更加健壮。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~