springboot 根据mvc请求编写测试用例

版权声明:此博客为个人博客,不涉及商业用途,仅提供学习参考,内容均来自个人原创以及互联网转载和摘录。 --------------------- 本文来自 路西法Lucifer 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/qq_37495786/article/details/83044159

user:实体类


/**
 * @author: Lucifer
 * @create: 2018-10-14 00:29
 * @description:
 **/
public class User {

    public interface UserSimpleView{}
    public interface UserDetailView extends UserSimpleView{}

    private Integer id;
    private String name;
    private String password;

    private Date birthDate;

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    @JsonView(UserSimpleView.class)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @JsonView(UserSimpleView.class)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @JsonView(UserDetailView.class)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

UserController :

/**
 * @author: Lucifer
 * @create: 2018-10-14 00:27
 * @description:
 **/
@RestController
@RequestMapping(value = "/user")
public class UserController {


    @PostMapping
    public User create(@RequestBody  User user){
        //@RequestBody 如果此处不用这个注解的话,测试用例中相应的对象的值就是null
        //该注解用于读取Request请求的body部分数据,然后把相应的数据绑定到要返回的对象上; 
        System.out.println(user.getBirthDate());
        System.out.println(user.getId());
        System.out.println(user.getName());
        System.out.println(user.getPassword());

        user.setId(1);
        return user;
    }


    @GetMapping
    @JsonView(User.UserSimpleView.class)
    public List<User> query(UserQueryCondition queryCondition, Pageable pageable) {

        System.out.println(ReflectionToStringBuilder.toString(queryCondition, ToStringStyle.MULTI_LINE_STYLE));


        //Pageable 用于分页
        System.out.println("Pageable pageable:" + pageable.getPageNumber());
        System.out.println("Pageable pageable:" + pageable.getPageSize());
        System.out.println("Pageable pageable:" + pageable.getSort());

        List<User> users = new ArrayList<>();
        users.add(new User());
        users.add(new User());
        users.add(new User());
        return users;
    }

    //   \\d+这是正则表达式
    @GetMapping(value = "/{id:\\d+}")
    @JsonView(User.UserDetailView.class)
    public User getInfo(@PathVariable String id){
        User user=new User();
        user.setName("lucifer");
        return user;
    }

}

springboot测试类:

/**
 * @author: Lucifer
 * @create: 2018-10-14 00:15
 * @description:
 **/
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void whenQuerySuccess() throws Exception {
        String result = mockMvc.perform(get("/user")
                .param("name", "lucifer")
                .param("age", "16")
                .param("ageTo", "60")
                .param("xxx", "yyy")
                .param("size", "15")
                .param("page", "3")
                .param("sort", "age,desc")
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.length()").value(3))
                .andReturn().getResponse().getContentAsString();
        System.out.println("result:" + result);
    }

    @Test
    public void whenGenInfoSuccess() throws Exception {
        String result = mockMvc.perform(get("/user/1")
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.name").value("lucifer"))
                .andReturn().getResponse().getContentAsString();
        System.out.println("result:" + result);
    }

    @Test
    public void whenGetInfo() throws Exception {
        mockMvc.perform(get("/user/a")
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().is4xxClientError());
    }


    @Test
    public void whenCreateSuccess() throws Exception {
        Date date=new Date();
        System.out.println(date.getTime());
        String content = "{\"name\":\"lucifer\",\"password\":null,\"birthDate\":"+date.getTime()+"}";
        String result=mockMvc.perform(post("/user")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(content))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.id").value("1"))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);

    }

}

ps:  jsonPath资料: 

猜你喜欢

转载自blog.csdn.net/qq_37495786/article/details/83044159