java接口自动化测试框架搭建

本文介绍了如何使用RestAssured库进行HTTP接口的GET和POST请求,包括设置请求参数、触发请求、断言响应状态码,并展示了JUnit框架下创建和执行测试用例的步骤,如Calculate类的加减乘除运算测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.restassured进行接口请求

1.get方法请求接口并获取返回response对象

import static io.restassured.RestAssured.given;
import io.restassured.response.Response;

public class RestDemo {
    @Test
    public void testGetHtml(){
        Response response = 
        given().
                log().all().
                param("wd", "豆瓣").
        when().
                get("http://www.baidu.com/").
        then().
                log().all().
                statusCode(200).
        extract().
                response();
                
}
given():一次网络请求所需要的条件都写在这里,头信息、query参数
when():触发条件
then():断言
extract():提取返回值

2.post方法请求接口并获取返回response对象

import static io.restassured.RestAssured.given;
import io.restassured.response.Response;

public class RestDemo {
    @Test
    public void testGetHtml(){
        Response response = 
        given().
                log().all().
                body().
                param("wd", "豆瓣").
        when().
                post("http://www.baidu.com/").
        then().
                log().all().
                statusCode(200).
        extract().
                response();
                
}
given():一次网络请求所需要的条件都写在这里,头信息、query参数
when():触发条件
then():断言
extract():提取返回值

二.junit执行测试用例

1.新建Calculate类

package com.coke.util;

public class Calculate {

    public int add(int a,int b){
        return a+b;
    }

    public int sub(int a,int b){
        return  a-b;
    }

    public int mul(int a,int b){
        return a * b;
    }

    public int div(int a,int b){
        return a / b;
    }

}

2.在同一个包下新建CalculateTest类,然后便可在idea中运行

package com.coke.util;
import org.junit.*;
public class CalculateTest {

    @Test
    public void testAdd(){
        int result = new Calculate().add(1,2);
        Assert.assertEquals(3,result);
    }

    @Test
    public void testSub(){int result = new Calculate().sub(4,1);
        Assert.assertEquals(3,result);
        System.out.println("123");
    }

    @Test
    public void testDiv(){
        int result = new Calculate().div(8,2);
        Assert.assertEquals(4,result);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值