1. 程式人生 > 其它 >介面自動化:java+springboot+Junit+mybatis+mysql

介面自動化:java+springboot+Junit+mybatis+mysql

簡介:

開發語言:Java 框架:spring boot + MyBatis-plus + Junit 程式碼管理:git Java框架使用spring boot + Mybatis-plus + Junit spring boot管理配置依賴注入 mybatis-plus管理sql操作 junit管理測試用例 檔案結構: src => 程式碼主目錄 pom.xml => maven配置專案需要的jar包依賴 src/main 主要的程式碼都在這裡面 包括一些配置 封裝 資料庫等 src/main/java/com/huaxi api :介面呼叫路徑 base:封裝的get,post請求以及一些基礎方法 commom:一些公用的方法 config : 公共的一些引數配置 dao:DataAccessobjects 資料存取物件,封裝對資料庫的一些操作 entity: entity 一個類代表一張表的欄位對映 service : 封裝用例或者對於一些流程用例進行封裝 src/test/resources => mapper Mapper.xml對映檔案中定義了操作資料庫的sql,每個sql是一個statement,對映檔案是mybatis的核心 application.yml 資料庫連線相關配置,mybatis配置 generatorConfig.xml 資料庫關係程式碼生成器(小工具)配置 applicationContext.xml.properties 專案配置 redis.properties redis配置 src/test 存放測試用例 pom.xml maven的依賴包

目錄結構如圖:

一、環境準備

1、安裝jdk:

安裝包官網上下載:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html 選1.8的就可以,其他的沒有嘗試過

安裝配置教程可參考:https://www.cnblogs.com/kelley-2020/p/12786104.html 也可以自行百度,遍地都是教程

檢視java是否安裝成功的命令:Windows + R鍵,輸入cmd,進入命令列介面 輸入命令:java -version

2、安裝Git:

Git下載官方地址為:https://git-scm.com/download/win

安裝配置教程可參考:

https://www.cnblogs.com/ximiaomiao/p/7140456.html 當然也可以自己去百度

3、安裝IDEA:

官網下載地址:https://www.jetbrains.com/idea/

安裝教程可參考:http://c.biancheng.net/view/7592.html 自己百度也可以

4、下載Maven:

下載配置教程可參考:https://www.cnblogs.com/liuhongfeng/p/5057827.html

二、程式碼管理和提交

1、克隆程式碼到本地

git clonehttps://github.com/wen-5heht/huaxi-test.git

2、使用idea 開啟對應的專案,檢視專案,編寫用例

三、模組

1、http型別的post介面請求

BaseRequest:post請求,使用的springboot的restTemplate
 *restTemplatePostString
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public String restTemplatePostString(String url,Map<String, String> params) throws Exception{

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("token",getTokenWeb());

        org.springframework.http.HttpEntity<Map<String, String>> request = new org.springframework.http.HttpEntity<>(params, headers);
        ResponseEntity<String> response = restTemplate.postForEntity( url, request , String.class );
        JSONObject json = JSONObject.parseObject(response.getBody());
        return JSON.toJSONString(json, SerializerFeature.PrettyFormat);
    }

service:封裝介面呼叫

package com.huaxi.service;

import com.huaxi.api.ImageCloudAPI;
import com.huaxi.base.BaseRequest;
import com.huaxi.config.config;
import com.huaxi.vo.ListExaminationVO;
import com.huaxi.vo.QueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class ImageCloudService extends config {
    @Autowired
    BaseRequest baseRequest;

    /**
     * 數字影像簡介頁面
     * @param appCode
     * @param organCode
     * @param channelCode
     * @param appVersionCode
     * @param pageCode
     * @return
     * @throws Exception
     */
    public String imageCloudPage(String appCode,String organCode,String channelCode,String appVersionCode,String pageCode) throws Exception{
        HashMap<String,String> params = new HashMap<>();
        params.put("appCode",appCode);
        params.put("organCode",organCode);
        params.put("channelCode",channelCode);
        params.put("appVersionCode",appVersionCode);
        params.put("pageCode",pageCode);
        String result = baseRequest.restTemplatePostString(ImageCloudAPI.Image_Cloud_Page,params);
        return result;
    }


}

api:可以直接用完整的URL,也可以根據域名+url

package com.huaxi.api;

public interface ImageCloudAPI {
    /**
     * 數字影像簡介頁面
     */
    String Image_Cloud_Page = "https://hxgyapipre.cd120.info/cloud/hosplatcustomer/resource/page/query";

 
}

case:用例層,直接呼叫service的方法

package com.huaxi.huaxitest;

import com.huaxi.base.BaseRequest;
import com.huaxi.base.BaseTest;
import com.huaxi.base.CheckPoint;
import com.huaxi.service.ImageCloudService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;


@Slf4j
public class ImageCloudTestCase extends BaseTest {
    @Autowired
    ImageCloudService imageCloudService;
    @Autowired
    BaseRequest baseRequest;

    @Test
    public void test001() throws Exception{
        String appCode = "HXGYAPP";
        String organCode = "PUBLIC";
        String channelCode = "PATIENT_WECHAT";
        String appVersionCode = "1.0.0";
        String pageCode = "image_cloud_page";
        String result = imageCloudService.imageCloudPage(appCode,organCode,channelCode,appVersionCode,pageCode);
        log.info("影像雲簡介頁返回內容:" + result);
    }
}