Mock測試之Moco框架環境搭建及使用

一、前言

Mock測試:在測試過程中,對於某些不容易構造或者不容易獲取的比較複雜的對象,用一個虛擬的對象(Mock 對象)來創建以便測試的測試方法。在實際工作中,後端接口未開發完成時,前端界面卻開發完成,可以使用Mock的方式,來給前端返回想要的數據

二、Moco框架環境搭建

2.1 下載jar包

2.2 創建json文件

  • 在目錄下創建一個json文件,並配置請求響應即可
    在這裏插入圖片描述
    [
      {
        "description": "第一個接口",
        "request": {
          "uri":"/firstDemo"
        },
        "response": {
          "text": "firstDemo"
        }
      }
    ]
    

2.3 啓動服務

  • 命令格式
    java -jar jar包路徑 http -p 監聽端口 -c json配置文件
    
  • 實際效果
    java -jar  ./moco-runner-0.11.1-standalone.jar http -p 9090 -c first.json
    
    在這裏插入圖片描述
  • 提示:使用Ctrl+C命令可以退出並停止服務,如果需要修改json文件內容,修改保存後會自動重啓服務,無須自己重啓。

2.4 效果展示

  • 由於上面配置的不是Post請求,可以直接 http://localhost:端口號/URI訪問,並在界面上查看響應結果。
    在這裏插入圖片描述

2.5 響應中文顯示問題

  • 如果響應信息中有中文,爲了中文顯示正常,在response中添加下面內容即可。
    "headers":{
    "Content-Type":"text/html;charset=gbk"
    }
    

三、Mock框架使用

Moco框架的基本使用就是上面這些內容,關於詳細的各種請求的配置方式,可以參考官方文檔:這是官方文檔

3.1 Get請求

  • 在request中添加"method": "get"即可。

    {
    "description": "這是一個get請求的demo",
    "request": {
      "uri": "/getDemo",
      "method": "get"
    },
    "response": {
      "text": "這是一個沒有參數的get請求",
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
    

3.2 帶參數的Get請求

  • 在request中添加"queries": {}即可。
    {
    "description": "這是一個帶參數的Get請求的Demo",
    "request": {
      "uri": "/getWithParam",
      "method": "get",
      "queries": {
        "name": "張三",
        "sex": "20"
       }
     },
    "response": {
      "text": "我張三會來了",
      "headers":{
        "Content-Type":"text/html;charset=gbk"
       }
     }
    }
    

3.3 Post請求

  • 在request中添加"method": "post"即可。

    {
    "description": "這是一個不帶參數的post請求",
    "request": {
      "uri": "/postDemo",
      "method": "post"
     },
    "response": {
      "text": "這是一個不帶參數的Post請求",
      "headers":{
        "Content-Type":"text/html;charset=gbk"
       }
     }
    }
    

3.4 帶參數的Post請求

  • 在request中添加"forms": {}即可。
    {
    "description": "這是一個帶參數的post請求",
    "request": {
      "uri": "/postWithParam",
      "method": "post",
      "forms": {
        "name": "張三",
        "sex": "man"
      }
     },
    "response": {
      "text": "我張三帶着參數回來了",
      "headers":{
        "Content-Type":"text/html;charset=gbk"
       }
     }
    }
    

3.5 返回cookie信息的Get請求

  • 返回信息response中添加"cookies":{}即可
    {
    "description":"這是一個會返回cookie信息的get請求",
    "request": {
      "uri": "/getCookies",
      "method": "get"
     },
    "response": {
      "cookies": {
        "login": "true"
       },
      "text": "恭喜你獲得cookies信息成功",
      "headers":{
        "Content-Type":"text/html;charset=gbk"
       }
     }
    }
    

3.6 帶cookie信息的get請求

  • 在請求信息request中添加"cookies":{}即可。
    {
    "description": "這是一個帶有cookie信息才能訪問的get請求",
    "request": {
      "method": "get",
      "uri": "/getWithCookies",
      "cookies": {
        "login": "true"
       }
     },
    "response": {
      "text": "這是一個需要攜帶cookies才能訪問get請求",
      "headers":{
        "Content-Type":"text/html;charset=gbk"
       }
     }
    }
    

3.7 帶cookie信息的post請求

  • 在請求信息request中添加"cookies":{}即可。
    {
    "description": "這是一個需要帶有cookie信息才能訪問的post請求",
    "request": {
      "method": "post",
      "uri": "/postWithCookie",
      "cookies": {
        "login":"true"
      },
      "json": {
        "name": "張三",
        "age": "18"
      }
     },
    "response": {
      "status":200,
      "json": {
        "name":"張三",
        "status": "1"
      }
     }
    }
    

3.8 帶header信息的post請求

  • 在請求request中添加"headers":{}即可。
    {
    "description": "這是一個帶header信息的post請求",
    "request": {
      "method": "post",
      "uri": "/postWithHeaders",
      "json": {
        "a":"success",
        "status": "1"
      },
      "headers": {
        "Content-Type": "application/json"
      }
    },
    "response": {
      "json": {
        "李四":"success",
        "status": "1"
      }
     }
    }
    

四、代碼彙總下載

上面這些各種請求的情況,都彙總中一個json中了。且由於post請求在頁面中驗證不了是否成功,使用jmeter驗證接口是否成功。

  • 百度雲下載:百度雲下載 提取碼:l8fo
    在這裏插入圖片描述
    在這裏插入圖片描述
    參考鏈接彙總(自用):

https://blog.csdn.net/lt326030434/article/details/80339397
https://blog.csdn.net/qq_41829492/article/details/88093147
https://blog.csdn.net/wangyiyice/article/details/53586012
https://www.cnblogs.com/hanschen-coder/p/6528829.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章