Я только начинаю функциональное тестирование с использованием Rest-assured и API
В Test1 я делаю то, что обычно делаю с «бессонницей», я аутентифицируюсь и мне выдан токен доступа,
В Test2 Затем я хочу запустить API с токеном на предъявителя (как я это делаю в «Бессоннице»)
, но не могу понять, как сохранить его в Test1 как строку «accesssToken» и передать его в Test2 правильно .
package api_test;
import org.json.JSONObject;
import org.junit.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
public class Api_Authentication {
@Test(priority=1)
public void testauth() {
int code = RestAssured.given()
.auth().preemptive()
.basic("username", "password")
.when()
.get("https://www.somedomain.com/client_credential/accesstoken?grant_type=client_credentials")
.getStatusCode();
System.out.println("Response Code is : " + code);
Assert.assertEquals(code, 200);
}
@Test(priority=2)
public void testbody() {
Response resp = RestAssured
.given()
.headers(
"Authorization",
"Bearer "+ "accessToken",
"Content-Type",
ContentType.JSON,
"Accept",
ContentType.JSON)
.get("https://www.somedomain.com/customers?page=1&pageSize=50&name=smith");
String data = resp.asString();
System.out.println("Response = " + data);
System.out.println("Response = " + resp.getTime());
}
}