接口自动化:java+springboot+Junit/TestNG+mybatis-plus+mysql


简介:

开发语言:Java
框架:spring boot + MyBatis-plus + Junit
代码管理:git
开发集成工具:IntelliJ IDEA
 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 clone https://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,String token) 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",token);

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);
}

登录接口获取token:

package com.huaxi.base;

import com.alibaba.fastjson.JSONObject;
import com.huaxi.api.LoginApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @title:
 * @author: 2021/8/2017:57
 * @date: 2021/8/2017:57
 */
@Slf4j
@Service
public class LoginService {


    /**
     * 大众端预发布环境账号登录
     * @param accountNo
     * @return
     */
    public String AppLogin(String accountNo){
        HashMap<String ,String> params = new HashMap<>();
        params.put("accountNo",accountNo);
        params.put("loginDeviceNum","Atce-SFvOw-nIUpUwdjMN7hHSUChBgfAm2o6PwVoQUIL");
        params.put("loginDeviceType","APP");
        params.put("loginDeviceUUID","c50e03b5-707f-4498-b1ac-9951d855ae29");
        params.put("password","ZTz2K9uhlvwWxYtVdQsQDxjN3Le7keyWd018RUv+vTcPtZTs4P44ECdoThBKAtJthMbRDOdIzcDkC44M0rm8c86hLSgp9yFrjWWHSxkRY+zHqxGo8v8ymAjX2Wf/S+UNAgwI4sB7mnA7ahBOoCU1RRRCGpH6fBI+aUk+2Z5viLU=");
        params.put("appCode","HXGYAPP");
        params.put("channelCode","PATIENT_ANDROID");
        log.info("\n===============打印登录接口的入参:===================\n" + params);

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

        org.springframework.http.HttpEntity<Map<String, String> >request = new org.springframework.http.HttpEntity<>(params, headers);
        ResponseEntity<String> response = restTemplate.postForEntity( LoginApi.Account_Login, request , String.class );
        JSONObject jsonObject = JSONObject.parseObject(response.getBody());
        String token =jsonObject.getJSONObject("data").getString("token");
        log.info("\n===================打印登录接口返回的token:===================\n" + token);
        return token;
    }
}

 springboot启动类框架配置加载:

package com.huaxi.huaxitest;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@ServletComponentScan
@ImportResource(locations = {"classpath*:applicationContext*.xml"})
@EnableScheduling
@MapperScan("com.huaxi.dao")
public class HuaxitestApplication {

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
        return builder.sources(HuaxitestApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(HuaxitestApplication.class);
        springApplication.addListeners(new ApplicationPidFileWriter());
        springApplication.run(args);
    }

}

 

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,String token) 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,token);
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的方法,继承BaseTest
BaseTest:

package com.huaxi.base;

import com.huaxi.huaxitest.HuaxitestApplication;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = HuaxitestApplication.class)
public class BaseTest {
}

用例:

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 token = loginService.AppLogin("18683571131");
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,token);
log.info("影像云简介页返回内容:" + result);
CheckPoint.setResult(result);
CheckPoint.assertEquals("对比msg","操作成功","$.msg");
CheckPoint.assertEquals("对比code","1","$.code");
CheckPoint.assertEquals("对比errCode","0","$.errCode");
CheckPoint.check();
}
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM