单元测试

2021-09-28 11:37:05 浏览数 (1)

简介

@RequestBody方式

Controller

代码语言:javascript复制
/**
 * 药品索引--通过doctorTeamHot
 * @param searchRequest
 * @return
 */
@RequestMapping(value = "/searchDrugSpu")
public RpcResponse searchDrugSpu(@RequestBody DrugSearchRequest searchRequest) {

    Integer size = searchRequest.getLimit();
    Integer offset = searchRequest.getOffset();

    SearchResponse response = drugService.searchDrugSpu(searchRequest);

    log.debug(RESULT_DATA, response);
    return RpcResponse.success(ResponseUtil.searchResponse(response));
}

测试用例

代码语言:javascript复制
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SearchProxyApplication.class)
public class DrugControllerTest extends SearchProxyTest{

    @Test
    public void searchDrugSpu() throws Exception{

        url = "/drug/searchDrugSpu";
        json = "{n"  
                "    "keyword": "注射用多索茶碱",n"  
                "    "limit": 20,n"  
                "    "offset": 0n"  
                "}";

        ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.post(url)
                .contentType(MediaType.APPLICATION_JSON).content(json));
        MvcResult mvcResult = resultActions.andReturn();
        String result = mvcResult.getResponse().getContentAsString();

        log.info("返回的数据:{}", result);

        Response response = new Gson().fromJson(result, Response.class);
        Assert.assertTrue(response.getData() != null);
    }
}

@RequestParam方式

Controller

代码语言:javascript复制
/**
 * 患者版首页--去挂号
 *
 * @param requestData
 * @return
 */
@PostMapping(value = "/searchAllDoctorsForGuahao")
@ApiOperation(value = "searchAllDoctorsForGuahao接口", notes = "/doctor/searchAllDoctorsForGuahao")
public RpcResponse searchAllDoctorsForGuahao(@RequestParam DoctorSearchRequest requestData) {

    log.info(REQUEST_DATA, requestData);

    SearchResponse response = doctorSearchService.searchAllDoctorsForGuahao(requestData);

    log.debug(RESULT_DATA, response);
    return RpcResponse.success(ResponseUtil.searchResponse(response));
}

测试用例

代码语言:javascript复制
/**
 * 单元测试-searchAllDoctorByName
 */
@Test
public void searchAllDoctorByName() throws Exception{

    url = "/doctor/searchAllDoctorByName";
    json = "{"q":"王贵松","offset":"0","limit":"20"}";

    ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.post(url)
            .param("requestData", json));
    MvcResult mvcResult = resultActions.andReturn();
    String result = mvcResult.getResponse().getContentAsString();

    log.info("返回的数据:"   result);

    Response response = new Gson().fromJson(result, Response.class);
    Assert.assertTrue(response.getCode() == 0);
}

0 人点赞