首页 > 编程语言 >第 5 课、java和rest-assured

第 5 课、java和rest-assured

时间:2023-02-15 00:44:37浏览次数:60  
标签:java log assured get res rest log4j org appender

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package com.les.java.day01;
import org.apache.log4j.Logger;

public class Tester01 {
  private static Logger logger = Logger.getLogger(Tester01.class);
  public static void main(String[] args) {
    // TODO Auto-generated method stub
      logger.info("这是我第一行日志");

  }

}
log4j.properties配置文件:
--



#### set log levels ###
log4j.rootLogger = info, stdout , D
#
####  output to the console ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{HH:mm:ss}-[%p] [%c] - %m%n
#
#### Output to the log file ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ${springmvc.root}/WEB-INF/logs/debug.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}-[%p] [%l] - %m%n
#
#### Output to the log file ###
log4j.appender.I = org.apache.log4j.DailyRollingFileAppender
log4j.appender.I.File = ${springmvc.root}/WEB-INF/logs/info.log
log4j.appender.I.Append = true
log4j.appender.I.Threshold = INFO
log4j.appender.I.layout = org.apache.log4j.PatternLayout
log4j.appender.I.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}-[%p] [%l] - %m%n
#
#### Save exception information to separate file ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = ${springmvc.root}/WEB-INF/logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}-[%p] [%l] - %m%n

 

 

 

<dependencies>  
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>4.3.0</version>
        <scope>test</scope>
    </dependency>
     <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.9.10</version>
        <scope>test</scope>
    </dependency>    
</dependencies> 


RestAssuredDemo.java:
--------------------------------------- package com.test.day01; import io.restassured.RestAssured; import org.testng.annotations.Test; import java.io.File; import static io.restassured.RestAssured.given; public class RestAssuredDemo { @Test public void firstGetRequest(){ given(). //设置请求:请求头、请求体...  when(). get("https://www.baidu.com"). then(). log().body(); } @Test public void getDemo01(){ given(). queryParam("mobilephone","13323234545"). queryParam("pwd","123456"). when(). get("http://www.httpbin.org/get"). then(). log().body(); } @Test public void postDemo01(){ given(). formParam("mobilephone","13323234545"). formParam("pwd","123456"). contentType("application/x-www-form-urlencoded"). when(). post("http://www.httpbin.org/post"). then(). log().body(); } @Test public void postDemo02(){ String jsonData="{\"mobilephone\":\"13323234545\",\"pwd\":\"123456\"}"; given(). body(jsonData). contentType("application/json"). when(). post("http://www.httpbin.org/post"). then(). log().body(); } @Test public void postDemo03(){ String xmlData="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<suite>\n" + " <class>测试xml</class>\n" + "</suite>"; given(). body(xmlData). contentType("application/xml"). when(). post("http://www.httpbin.org/post"). then(). log().body(); } @Test public void postDemo04(){ given(). multiPart(new File("D:\\KuGou\\text.txt")). when(). post("http://www.httpbin.org/post"). then(). log().body(); } }

 

GetResponse.java:

package com.test.day01;

import io.restassured.response.Response;
import org.testng.annotations.Test;

import java.util.List;

import static io.restassured.RestAssured.given;

public class GetResponse {
    @Test
    public void getResponseHeader(){
        Response res =
            given().
            when().
                    post("http://www.httpbin.org/post").
            then().
                    log().all().extract().response();
        System.out.println("接口的响应时间:"+res.time());
        System.out.println(res.getHeader("Content-Type"));
    }

    @Test
    public void getResponseJson01(){
        String json = "{\"mobile_phone\":\"13323231111\",\"pwd\":\"12345678\"}";
        Response res =
                given().
                        body(json).
                        header("Content-Type","application/json").
                        header("X-Lemonban-Media-Type","lemonban.v1").
                when().
                        post("http://api.lemonban.com/futureloan/member/login").
                then().
                        log().all().extract().response();
        System.out.println(res.jsonPath().get("data.id"));
    }

    @Test
    public void getResponseJson02(){
        Response res =
                given().
                when().
                        get("http://www.httpbin.org/json").
                then().
                        log().all().extract().response();
        System.out.println(res.jsonPath().get("slideshow.slides.title"));
        List<String> list = res.jsonPath().getList("slideshow.slides.title");
        System.out.println(list.get(0));
        System.out.println(list.get(1));
    }

    @Test
    public void getResponseHtml(){
        Response res =
                given().
                when().
                        get("http://www.baidu.com").
                then().
                        log().all().extract().response();
        System.out.println(res.htmlPath().get("html.head.meta[0].@http-equiv"));
        System.out.println(res.htmlPath().get("html.head.meta[0].@content"));
        System.out.println(res.htmlPath().getList("html.head.meta"));
    }

    @Test
    public void getResponseXml(){
        Response res =
                given().
                when().
                        get("http://www.httpbin.org/xml").
                then().
                        log().all().extract().response();
        System.out.println(res.xmlPath().get("slideshow.slide[1].title"));
        System.out.println(res.xmlPath().get("slideshow.slide[1].@type"));
    }
--------
//登录充值
    @Test
    public void loginRecharge(){
        String json = "{\"mobile_phone\":\"13323231111\",\"pwd\":\"12345678\"}";
        Response res =
                given().
                        body(json).
                        header("Content-Type","application/json").
                        header("X-Lemonban-Media-Type","lemonban.v2").
                when().
                        post("http://api.lemonban.com/futureloan/member/login").
                then()
                        .extract().response();
        //1、先来获取id
        int memberId = res.jsonPath().get("data.id");
        System.out.println(memberId);
        //2、获取token
        String token = res.jsonPath().get("data.token_info.token");
        System.out.println(token);

        //发起“充值”接口请求
        String jsonData = "{\"member_id\":"+memberId+",\"amount\":100000.00}";
        Response res2 =
                given().
                        body(jsonData).
                        header("Content-Type","application/json").
                        header("X-Lemonban-Media-Type","lemonban.v2").
                        header("Authorization","Bearer "+token).
                when().
                        post("http://api.lemonban.com/futureloan/member/recharge").
                then().
                        log().all().extract().response();
        System.out.println("当前可用余额:"+res2.jsonPath().get("data.leave_amount"));
    }



}

 

 

 

 

 

 

标签:java,log,assured,get,res,rest,log4j,org,appender
From: https://www.cnblogs.com/pingzi66-ww/p/17121334.html

相关文章

  • java基础之Object中的toString()方法
    toString()方法返回该对象的字符串表示。该字符串由类名、@、和次对象哈希码值的16进制表示组成,换句话说就是原理分析:全限定路径:包名+类名hascode()---》将对象在堆中......
  • java基础之super
    super属性和方法指的是:父类super可以修饰属性,修饰方法。在子类的方法中,可以通过super属性,super方法的方式,显式的去调用父类的属性和犯法,在通常情况下,super.可以省略不写......
  • JavaConfig
    使用Java的方式配置spring我们现在完全不使用spring的xml配置了,全权交给Java来做!JavaConfig是spring的一个子项目,在spring4之后,它成为一个核心功能!1.实体类packageto......
  • Java报错:Failed to start bean ‘documentationPluginsBootstrapper‘
    场景描述SpringBoot引入knife4j+Swagger报错Failedtostartbean'documentationPluginsBootstrapper'引入版本<parent> <groupId>org.springframework.boot</grou......
  • Java:SpringBoot整合Knife4j(Swagger)提供接口文档
    spring-boot版本2.7.71、引入Maven坐标pom.xml<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId>......
  • JavaSE5️⃣核心类 - 包装类型
    1、包装类型Java数据类型基本类型:byte,short,int,longfloat,doublechar,boolean引用类型:八种基本类型之外的所有数据类型。包含所有class和interface类型。......
  • Java 发展史
    Java语言的诞生具有一定的戏剧性,它并不是经过精心的策划,制作,最后产生的划时代产品。从某种角度来看,java语言的诞生完全是一种误会。C++的局限性是Java诞生的客观原因1990年......
  • JavaScript模块化方案的演进史
    JavaScript语言诞生至今,模块规范化之路曲曲折折。社区先后出现了各种解决方案,包括AMD、CMD、CommonJS等,而后ECMA组织在JavaScript语言标准层面,增加了模块功能(因为该......
  • 基于JAVA+SpringBoot+VUE的心理健康测试系统的设计与实现
    ✌全网粉丝20W+,csdn特邀作者、博客专家、CSDN新星计划导师、java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战......
  • Java重写与重载
    Java方法重写Override与重载Overload介绍如果一个类多个相同名字但是参数个数不同,我们称之为重载Overload;如果一个类和它的父类方法名相同、参数相同、返回类型相同、但......