一.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);
    }
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐