• SpringBoot自动生成测试案例


    ---恢复内容开始---

    @SpringBootTest注解是SpringBoot自1.4.0版本开始引入的一个用于测试的注解

    1.添加maven依赖

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-test</artifactId>

    </dependency>

    2.Controller右键->new一个test case,选择自己要生成案例的方法,点击完成。

     

    3.使用MockMvc进行单元测试,MockMvc是唯一支持Controller测试的。

    测试类如下:

    1. private MockMvc mockMvc;  
    2.   
    3.    @Autowired  
    4.    private WebApplicationContext webApplicationContext;  
    5.   
    6.    @Before  
    7.    public void init() {  
    8.        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();  
    9.    }  
    10.   
    11.    @Test  
    12.    public void testRegister() {  
    13.        // fail("Not yet implemented");  
    14.        UusRegisterReq req = new UusRegisterReq();  
    15.        req.setParterKey("KE");  
    16.        req.setParterSource(2);  
    17.        req.setParterUesrId("1");  
    18.        req.setPhone("13122221111");  
    19.   
    20.        String data = FastJsonUtils.obj2json(req);  
    21.        RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/hehe/register").content(data)  
    22.                .contentType(MediaType.APPLICATION_JSON);  
    23.        try {  
    24.            String res = mockMvc.perform(requestBuilder).andExpect(MockMvcResultMatchers.status().isOk()).andReturn()  
    25.                    .getResponse().getContentAsString();  
    26.            log.info("注册测试结果:{}", res);  
    27.        } catch (Exception e) {  
    28.            e.printStackTrace();  
    29.        }  
    30.    }  
  • 相关阅读:
    PS选区认识
    移动工具
    PS认识及新建文件
    第02组 Alpha冲刺(3/4)
    第02组 Alpha冲刺(2/4)
    第02组 Alpha冲刺(1/4)
    第02组 团队Git现场编程实战
    第二次结对编程作业
    团队项目-需求分析报告
    团队项目-需求分析报告
  • 原文地址:https://www.cnblogs.com/lucky-girl/p/9477483.html
Copyright © 2020-2023  润新知