zoukankan      html  css  js  c++  java
  • springboot1.5.x 测试sample

      1 /**
      2  * 
      3  */
      4 package com.imooc.web.controller;
      5 
      6 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
      7 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
      8 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
      9 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
     10 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
     11 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
     12 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
     13 
     14 import java.time.LocalDateTime;
     15 import java.time.ZoneId;
     16 import java.util.Date;
     17 
     18 import org.junit.Before;
     19 import org.junit.Test;
     20 import org.junit.runner.RunWith;
     21 import org.springframework.beans.factory.annotation.Autowired;
     22 import org.springframework.boot.test.context.SpringBootTest;
     23 import org.springframework.http.MediaType;
     24 import org.springframework.mock.web.MockMultipartFile;
     25 import org.springframework.test.context.junit4.SpringRunner;
     26 import org.springframework.test.web.servlet.MockMvc;
     27 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
     28 import org.springframework.web.context.WebApplicationContext;
     29 
     30 /**
     31  * @author zhailiang
     32  *
     33  */
     34 @RunWith(SpringRunner.class)
     35 @SpringBootTest
     36 public class UserControllerTest {
     37 
     38     @Autowired
     39     private WebApplicationContext wac;
     40 
     41     private MockMvc mockMvc;
     42 
     43     @Before
     44     public void setup() {
     45         mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
     46     }
     47     
     48     @Test
     49     public void whenUploadSuccess() throws Exception {
     50         String result = mockMvc.perform(fileUpload("/file")
     51                 .file(new MockMultipartFile("file", "test.txt", "multipart/form-data", "hello upload".getBytes("UTF-8"))))
     52                 .andExpect(status().isOk())
     53                 .andReturn().getResponse().getContentAsString();
     54         System.out.println(result);
     55     }
     56     
     57 
     58     @Test
     59     public void whenQuerySuccess() throws Exception {
     60         String result = mockMvc.perform(
     61                 get("/user").param("username", "jojo").param("age", "18").param("ageTo", "60").param("xxx", "yyy")
     62                         // .param("size", "15")
     63                         // .param("page", "3")
     64                         // .param("sort", "age,desc")
     65                         .contentType(MediaType.APPLICATION_JSON_UTF8))
     66                 .andExpect(status().isOk()).andExpect(jsonPath("$.length()").value(3))
     67                 .andReturn().getResponse().getContentAsString();
     68         
     69         System.out.println(result);
     70     }
     71 
     72     @Test
     73     public void whenGetInfoSuccess() throws Exception {
     74         String result = mockMvc.perform(get("/user/1")
     75                 .contentType(MediaType.APPLICATION_JSON_UTF8))
     76                 .andExpect(status().isOk())
     77                 .andExpect(jsonPath("$.username").value("tom"))
     78                 .andReturn().getResponse().getContentAsString();
     79         
     80         System.out.println(result);
     81     }
     82     
     83     @Test
     84     public void whenGetInfoFail() throws Exception {
     85         mockMvc.perform(get("/user/a")
     86                 .contentType(MediaType.APPLICATION_JSON_UTF8))
     87                 .andExpect(status().is4xxClientError());
     88     }
     89     
     90     @Test
     91     public void whenCreateSuccess() throws Exception {
     92         
     93         Date date = new Date();
     94         System.out.println(date.getTime());
     95         String content = "{"username":"tom","password":null,"birthday":"+date.getTime()+"}";
     96         String reuslt = mockMvc.perform(post("/user").contentType(MediaType.APPLICATION_JSON_UTF8)
     97                 .content(content))
     98                 .andExpect(status().isOk())
     99                 .andExpect(jsonPath("$.id").value("1"))
    100                 .andReturn().getResponse().getContentAsString();
    101         
    102         System.out.println(reuslt);
    103     }
    104     
    105     @Test
    106     public void whenCreateFail() throws Exception {
    107         
    108         Date date = new Date();
    109         System.out.println(date.getTime());
    110         String content = "{"username":"tom","password":null,"birthday":"+date.getTime()+"}";
    111         String reuslt = mockMvc.perform(post("/user").contentType(MediaType.APPLICATION_JSON_UTF8)
    112                 .content(content))
    113 //                .andExpect(status().isOk())
    114 //                .andExpect(jsonPath("$.id").value("1"))
    115                 .andReturn().getResponse().getContentAsString();
    116         
    117         System.out.println(reuslt);
    118     }
    119     
    120     @Test
    121     public void whenUpdateSuccess() throws Exception {
    122         
    123         Date date = new Date(LocalDateTime.now().plusYears(1).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
    124         System.out.println(date.getTime());
    125         String content = "{"id":"1", "username":"tom","password":null,"birthday":"+date.getTime()+"}";
    126         String reuslt = mockMvc.perform(put("/user/1").contentType(MediaType.APPLICATION_JSON_UTF8)
    127                 .content(content))
    128                 .andExpect(status().isOk())
    129                 .andExpect(jsonPath("$.id").value("1"))
    130                 .andReturn().getResponse().getContentAsString();
    131         
    132         System.out.println(reuslt);
    133     }
    134     
    135     @Test
    136     public void whenDeleteSuccess() throws Exception {
    137         mockMvc.perform(delete("/user/1")
    138                 .contentType(MediaType.APPLICATION_JSON_UTF8))
    139                 .andExpect(status().isOk());
    140     }
    141 
    142 }
  • 相关阅读:
    CALayer3层的属性
    CALayer1简介
    网络处理2异步POST请求和同步请求
    网络处理1异步GET请求
    【ObjectiveC】05第一个OC的类
    CALayer4自定义层
    .Net网站不能预编译以及不能请求的调试过程
    (源码)WinForm TextBox 实现自动索引功能
    分享一个WinForm里面自定义的MessageBox
    C#操作十六进制数据以及十进制与十六进制互相转换
  • 原文地址:https://www.cnblogs.com/buxiu/p/13387742.html
Copyright © 2011-2022 走看看