zoukankan      html  css  js  c++  java
  • Junit5+REST-assured 做接口测试

    1. 先配置maven

     <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.6.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-params</artifactId>
                <version>5.7.0</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>io.rest-assured</groupId>
                <artifactId>rest-assured</artifactId>
                <version>3.0.3</version>
            </dependency>
            <dependency>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-junit5</artifactId>
                <version>2.13.2</version>
                <scope>test</scope>
            </dependency>

    2.调用企业微信的创建部门接口

    package com.wechat.department;
    
    import io.restassured.response.Response;
    import org.junit.jupiter.api.BeforeAll;
    import org.junit.jupiter.api.DisplayName;
    import org.junit.jupiter.api.Test;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import static io.restassured.RestAssured.given;
    
    /**
     * Created by CaiQingYuan on 2021/4/5 17:01
     */
    public class Demo_01_base {
        private static final Logger logger = LoggerFactory.getLogger(Demo_01_base.class);
    
        static String accessToken;
        static int departmentId;
        @BeforeAll
        public static void getAccessToken(){
            accessToken = given().log().all()
                    .when()
                    .param("corpid","*******")
                    .param("corpsecret","******")
                    .get("https://qyapi.weixin.qq.com/cgi-bin/gettoken")
                    .then().log().all()
                    .extract().response().path("access_token");
            logger.info(accessToken);
        }
        @DisplayName("创建部门")
        @Test
        void createDepartment(){
            String body = "{
    " +
                    ""name": "广州研发中心002",
    " +
                    "   "name_en": "RDGZ0002",
    " +
                    "   "parentid": 42,
    " +
                    "   "order": 1,
    " +
                    "   "id": 1006" +
                    "}
    ";
            Response response = given().log().all()
                    .contentType("application/json")
                    .body(body)
                    .post("https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token="+accessToken)
                    .then().log().all()
                    .extract()
                    .response();
            departmentId = response.path("id");
            logger.info(accessToken);
            logger.info(""+departmentId);
        }
    }
  • 相关阅读:
    [编程题]多多的数字组合
    mac传输文件到服务器
    git 清除缓存、查看add内容
    go build
    vim编辑器
    Git: clone指定分支
    查看端口占用以及kill
    curl小记录
    Python3.9 malloc error: can’t allocate region
    设计模式-策略模式
  • 原文地址:https://www.cnblogs.com/cythia2018/p/14619039.html
Copyright © 2011-2022 走看看