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 = "{\n" +
                "\"name\": \"广州研发中心002\",\n" +
                "   \"name_en\": \"RDGZ0002\",\n" +
                "   \"parentid\": 42,\n" +
                "   \"order\": 1,\n" +
                "   \"id\": 1006" +
                "}\n";
        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);
    }
}

 

上一篇:java8+junit5实现并发测试(多线程)


下一篇:Maven导入Junit 5