zoukankan      html  css  js  c++  java
  • test中测试 模拟浏览器 Spring MVC测试框架详解——服务端测试

    1get请求 参数跟在url后面的(常见为列表中每一行查询详情的时候,参数跟在a标签的路径后面的应用场景)

    get无参查询:

    @Test

           public void testSelectAuditStatus() throws Exception{

                  //purchaseItListController.selectAuditStatus();

          

                  String url = "/purchaseItList/selectAuditStatus";

            MvcResult result = (MvcResult) mockMvc.perform(get(url)

                  .contentType(MediaType.APPLICATION_JSON)

                  .param("epsToken", "token")

                  .accept(MediaType.APPLICATION_JSON))

                  .andExpect(status().isOk())

                  .andDo(MockMvcResultHandlers.print()).andReturn();

             Assert.assertEquals(200, result.getResponse().getStatus());

             Assert.assertNotNull(result.getResponse().getContentAsString());

           }

     

    @Controller

    @RequestMapping("/purchaseItList")

    public class PurchaseItListController implements Serializable{

           private static final long serialVersionUID = 2732216546553695880L;

           private static final Logger log = LoggerFactory.getLogger(ItEquipmentRepairCostMaintenanceController.class);

           @Resource

           private PurchaseItListService purchaseItListService;

           /**

            * query auditStatus list

            * 查询审核状态

            * @param id

            * @return

            */

           @RequestMapping(value = "/selectAuditStatus", method = RequestMethod.GET)

           @ResponseBody

           public ResultModel selectAuditStatus() {

                  log.info(this.getClass().getName()+".selectAuditStatus.start");

                  ResultModel resultModel = new ResultModel();

                  Map<String,String> statusMap=purchaseItListService.selectAuditStatus();

                  resultModel.setResultCode(Constants.SERVICE_SUCCESS_CODE);

                  resultModel.setResultMsg(Constants.DATA_BASE_SEARCH_SUCCESS_MSG);

                  resultModel.setResultData(statusMap);

                  log.info(this.getClass().getName()+".selectAuditStatus.end.resultModel="+resultModel);

                  System.out.print("==========resultModel="+resultModel);;

                  return resultModel;

           }

    get有参数测试:

    * 根据id查询详情

            * @throws Exception

            */

           //@Test

           public void selectByPrimaryKey() throws Exception {

                  String url = "/storeLocalPm/selectByPrimaryKey/{id}";

                  MvcResult result = (MvcResult) mockMvc.perform(

                                       //id:key  1value 对应controller中的key值。

                                    get(url,1)

                                   

                         .contentType(MediaType.APPLICATION_JSON)

                         .param("epsToken", "token")

                         .accept(MediaType.APPLICATION_JSON))

                         .andExpect(status().isOk())

                         .andDo(MockMvcResultHandlers.print())

                               .andReturn();

                  Assert.assertEquals(200, result.getResponse().getStatus());

            Assert.assertNotNull(result.getResponse().getContentAsString());

                 

           }

    /**

            * query detail by id

            * 根据id查询详情

            * @param id

            * @return

            */

           @RequestMapping(value = "/selectByPrimaryKey/{id}",

                                method = RequestMethod.GET,

                                consumes = MediaType.APPLICATION_JSON_VALUE)

           @ResponseBody

           public ResultModel selectByPrimaryKey(@PathVariable("id") Long id) {

                  log.info(this.getClass().getName()+".selectByPrimaryKey.start.id="+id);

                  ResultModel resultModel = new ResultModel();

                  StoreLocalPm storeLocalPm=new StoreLocalPm();

                  try{

                      storeLocalPm=storeLocalPmService.selectByPrimaryKey(id);

                      log.info(this.getClass().getName()+".selectByPrimaryKey.success.storeLocalPm="+storeLocalPm);

                      resultModel.setResultCode(Constants.SERVICE_SUCCESS_CODE);

                         resultModel.setResultMsg(Constants.DATA_BASE_SEARCH_SUCCESS_MSG);

                         resultModel.setResultData(storeLocalPm);

                  }catch(Exception e){

                         resultModel.setResultCode(Constants.SERVICE_ERROR_CODE);

                         resultModel.setResultMsg(Constants.DATA_BASE_SEARCH_ERROR_MSG);

                  }     

                  log.info(this.getClass().getName()+".selectByPrimaryKey.end.resultModel="+resultModel);

                  return resultModel;

           }

    2 post提交,一般用于非a标签的,也就是参数并不是跟在url后面的。而是在js中组织参数,一般是组织为一个对象,传过来的

    post无参测试:

    /**

            * 准备编辑数据到新增页面

            * @throws Exception

            */

           //@Test

           public void selectStoreLocalListForCreate() throws Exception {

    //            ResultModel resultModel=storeLocalPmController.selectStoreLocalListForCreate();

    //            System.out.println("selectStoreLocalListForCreate--------resultModel="+resultModel);

                  MvcResult result = (MvcResult) mockMvc.perform(                                  

                                    post("/storeLocalPm/selectStoreLocalListForCreate")

                         .contentType(MediaType.APPLICATION_JSON)

                         .param("epsToken", "token")

                         .accept(MediaType.APPLICATION_JSON))

                         .andExpect(status().isOk())

                         .andDo(MockMvcResultHandlers.print())

                               .andReturn();

                  Assert.assertEquals(200, result.getResponse().getStatus());

            Assert.assertNotNull(result.getResponse().getContentAsString());

                 

           }

    /**

            * prepare info for creatPage

            * 准备编辑数据到新增页面

            * @param id

            * @return

            */

           @RequestMapping(value = "/selectStoreLocalListForCreate",

                                method = RequestMethod.POST,

                                consumes = MediaType.APPLICATION_JSON_VALUE)

           @ResponseBody

           public ResultModel selectStoreLocalListForCreate() {

                  log.info(this.getClass().getName()+".selectStoreLocalListForCreate.start");

                  ResultModel resultModel = new ResultModel();

                  Map result=new HashMap();

                  try{

                         result=storeLocalPmService.selectStoreLocalListForCreate();

                      log.info(this.getClass().getName()+".selectStoreLocalListForCreate.success.result="+result);

                      resultModel.setResultCode(Constants.SERVICE_SUCCESS_CODE);

                         resultModel.setResultMsg(Constants.DATA_BASE_ADD_SUCCESS_MSG);

                         resultModel.setResultData(result);

                  }catch(Exception e){

                         resultModel.setResultCode(Constants.SERVICE_ERROR_CODE);

                         resultModel.setResultMsg(Constants.DATA_BASE_SEARCH_ERROR_MSG);

                  }     

              log.info(this.getClass().getName()+".selectStoreLocalListForCreate.end.resultModel="+resultModel);

                  return resultModel;

           }

    post有参测试:

    /**

            * 新增一条数据

            * @throws Exception

            */

           @Test

           public void testInsert() throws Exception {

    //            StoreLocalPm storeLocalPm=new StoreLocalPm ();

    //            storeLocalPm.setStoreNo("562");

    //            storeLocalPm.setMaintainLocalPm("设备经理。。。");

    //            storeLocalPm.setProjectLocalPm("工程经理。。。");

    //            storeLocalPm.setItLocalPm("it工程经理");

    //            storeLocalPm.setTaxpayer("纳税主体信息。。。");

    //            ResultModel resultModel=storeLocalPmController.insertStoreLocalPm(storeLocalPm);

    //            System.out.println("testInsert--------resultModel="+resultModel);

                 

                  String url = "/storeLocalPm/insertStoreLocalPm";

                 

                  StoreLocalPm storeLocalPm=new StoreLocalPm ();

                  storeLocalPm.setStoreNo("589");

                  storeLocalPm.setMaintainLocalPm("设备经理。。。");

                  storeLocalPm.setProjectLocalPm("工程经理。。。");

                  storeLocalPm.setItLocalPm("it工程经理");

                  storeLocalPm.setTaxpayer("纳税主体信息。。。");

            ObjectMapper mapper = new ObjectMapper();

         

            String json = mapper.writeValueAsString(storeLocalPm);

        

            MvcResult result = (MvcResult) mockMvc.perform(post(url)

                         .contentType(MediaType.APPLICATION_JSON)

                         .content(json)

                         .param("epsToken", "token")

                         .accept(MediaType.APPLICATION_JSON))

                         .andExpect(status().isOk())

                         .andDo(MockMvcResultHandlers.print())

                               .andReturn();

                    Assert.assertEquals(200, result.getResponse().getStatus());

            Assert.assertNotNull(result.getResponse().getContentAsString());

           }

    /**

            * insert StoreLocalPm

            * 新增一条数据

            * @param record

            * @return

            * @throws Exception

            */

            @RequestMapping(value = "/insertStoreLocalPm", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)

            @ResponseBody

            public ResultModel insertStoreLocalPm(@RequestBody StoreLocalPm storeLocalPm) throws Exception {

        log.info(this.getClass().getName()+".insertStoreLocalPm.start.storeLocalPm="+JSON.toJSONString(storeLocalPm));

           ResultModel result = new ResultModel();

                  int number=0;

                  storeLocalPm.setAuditStatus("0");//0未审核 1通过 2不通过

                  number= storeLocalPmService.insertStoreLocalPm(storeLocalPm);

                  if(number<1){

                               log.error(this.getClass().getName()+".insertStoreLocalPm.50010"+"新增一条数据number="+JSON.toJSONString(number));

                         throw new Exception("50010"+"新增失败");

                  }

                  result.setResultCode(Constants.SERVICE_SUCCESS_CODE);

                  result.setResultMsg(Constants.DATA_BASE_ADD_SUCCESS_MSG);

                  result.setResultData(number);

                  log.info(this.getClass().getName()+".insertStoreLocalPm.end.result="+result);

                  return result;

           }

  • 相关阅读:
    轻量级MVVM框架Stylet介绍:(13) ValidatingModelBase
    轻量级MVVM框架Stylet介绍:(9)PropertyChangedBase
    轻量级MVVM框架Stylet介绍:(8)事件聚合器
    轻量级MVVM框架Stylet介绍:(12) BindableCollection
    轻量级MVVM框架Stylet介绍:(14) StyletIoC
    轻量级MVVM框架Stylet介绍:(5) Actions
    轻量级MVVM框架Stylet介绍:(10) Execute:调度到UI线程
    轻量级MVVM框架Stylet介绍:(7) MessageBox
    轻量级MVVM框架Stylet介绍:(6) WindowManager
    tushare的mysql的sql结构文件
  • 原文地址:https://www.cnblogs.com/songyunxinQQ529616136/p/6262079.html
Copyright © 2011-2022 走看看