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