WEB单元测试编写

  • 引入单元的是模块
在项目引入单元测试包 - 以spring-boot项目为例
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
  • 编写单元测试类
@SpringBootTest(classes = SpringBootGitApplication.class)
@RunWith(SpringRunner.class)
public class SpringBootMockMvc {

    private MockMvc mockMvc;
    
    @Autowired
    private WebApplicationContext wac;
    
    @Before
    public void setup(){
        // 测试代码执行前,需要执行的代码
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test // 上传文件测试
    public void testFile() throws Exception {
        String result = mockMvc.perform(MockMvcRequestBuilders.multipart("/test/file")
                        // MockMultipartFile类参数 1.属性名,2.文件的名字,3.内容类型,4.上传内容字节
                        .file(new MockMultipartFile("file",
                                "test.txt",
                                "multipart/form-data",
                                "hello upload".getBytes())))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }

    @Test // get请求测试
    public void testGet() throws Exception {
        String result = mockMvc.perform(MockMvcRequestBuilders.get("/"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }

    @Test // post请求测试
    public void testPost() throws Exception {
        Date date = new Date();
        System.out.println(date.getTime());
        String content = "{\"username\":\"li wen ya\",\"password\":null,\"birthday\":"+date.getTime()+"}";
        String result = mockMvc.perform(MockMvcRequestBuilders.post("/user")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(content))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }
}
  • 接收类接口
@Controller
@RequestMapping("/test")
public class SpringTestController {

    @ResponseBody
    @GetMapping("/one")
    public String test(String name){
        System.out.println(name);
        return "hello world";
    }

    @ResponseBody
    @PostMapping("/user")
    public User testPost(@RequestBody User user){
        return user;
    }

    @ResponseBody
    @RequestMapping("/file")
    public String file(MultipartFile file){
        System.out.println(file.getName());
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getSize());
        return "成功";
    }
}
  • user对象

public class User {

    /**
     * 用户名称
     */
    private String username;

    /**
     * 用户密码
     */
    private String password;

    /**
     * 生日
     */
    private Date birthday;

    // get/set方法省略
}
  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值