码迷,mamicode.com
首页 > 编程语言 > 详细

springboot-单元测试编写

时间:2020-10-13 17:40:47      阅读:31      评论:0      收藏:0      [点我收藏+]

标签:auth   测试   request   ica   value   lib   work   autowire   moc   

package com.msun.drug.api.doctor;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

/**
 * @description:
 * @author: kevin
 * @create: 2020-09-08 15:27
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class DoctorPatientApiTest {
    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    // 在测试开始前初始化工作
    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void doctorRevokePatientDrugById() throws Exception {
        String id = "";
        mockMvc.perform(MockMvcRequestBuilders
                .post("/doctorPatientApi/doctorRevokePatientDrugById")
                .contentType(MediaType.APPLICATION_JSON)
                .param("id",id))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(jsonPath("$.success").value(false))
                .andDo(MockMvcResultHandlers.print());

        // 存在请求结果
        id = "1";
        mockMvc.perform(MockMvcRequestBuilders
                .post("/doctorPatientApi/doctorRevokePatientDrugById")
                .contentType(MediaType.APPLICATION_JSON)
                .param("id",id))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(jsonPath("$.success").value(false))
                .andDo(MockMvcResultHandlers.print());
    }

springboot-单元测试编写

标签:auth   测试   request   ica   value   lib   work   autowire   moc   

原文地址:https://www.cnblogs.com/zhenghengbin/p/13807213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!