In one class I get a code and an identifier in the method, and in another class I need to use them as input parameters. How to save the values ​​of smsCode and smsId variables for use in another class?

import io.restassured.RestAssured; import io.restassured.specification.RequestSpecification; import io.restassured.response.Response; import io.restassured.response.ResponseBody; import io.restassured.path.json.JsonPath; import org.json.JSONObject; import org.testng.Assert; import org.testng.annotations.Test; class GetSmsTest { Config config = new Config (); String smsCode; String smsId; @Test void getSMSTest () { RequestSpecification request = RestAssured.given ().relaxedHTTPSValidation (); Response response = request.get (config.getToken); JsonPath jsonPath = response.jsonPath (); String token = jsonPath.get ("access_token"); request.header ("Authorization", "Bearer " + token); JSONObject jsonObject = new JSONObject (); jsonObject.put ("recipient", "9031111112"); request.header ("Content-Type", "application/json; charset=UTF-8"); request.body (jsonObject.toString ()); response = request.post (config.getSMS); jsonPath = response.jsonPath (); smsCode = jsonPath.get ("code"); smsId = jsonPath.get ("id"); } } package qa.onlinebroker; import io.restassured.RestAssured; import io.restassured.specification.RequestSpecification; import io.restassured.response.Response; import io.restassured.response.ResponseBody; import io.restassured.path.json.JsonPath; import org.json.JSONObject; import org.testng.Assert; import org.testng.annotations.Test; class CheckSmsTest { Config config = new Config (); GetSmsTest getSmsTest = new GetSmsTest (); @Test void checkSMSTest () { RequestSpecification request = RestAssured.given ().relaxedHTTPSValidation (); Response response = request.get (config.getToken); JsonPath jsonPath = response.jsonPath (); String token = jsonPath.get ("access_token"); request.header ("Authorization", "Bearer " + token); JSONObject jsonObject = new JSONObject (); jsonObject.put ("code", getSmsTest.smsCode); jsonObject.put ("id", getSmsTest.smsId); request.header ("Content-Type", "application/json; charset=UTF-8"); request.body (jsonObject.toString ()); response = request.post (config.checkSMS); } } import io.restassured.RestAssured; import io.restassured.path.json.*; import io.restassured.response.*; import io.restassured.specification.*; class Config { void setToken (String token) { this.accToken = token; RequestSpecification request = RestAssured.given ().relaxedHTTPSValidation (); Response response = request.get (getToken); JsonPath jsonPath = response.jsonPath (); String access_token = jsonPath.get ("access_token"); request.header ("Authorization", "Bearer " + accToken); } } 
  • Write to the stack of values. - Roman C
  • Write to the stack of values. - Roman C
  • In sense in the stack of values? - user328512
  • Well, it's a long time to explain - Roman C

1 answer 1

Let us first try to define the concept itself:

Passing data to an object of another class is usually done or through a constructor (for example, the method method creates an object of class B and sends data from variables as to it)

 class A { int a = 1; String s= "str" void method() { new B(a, s) } } class B { int a; String s; B (int a, String s) { //конструктор this.a = a; this.s = s } } 

or via setter

 class A { int a = 1; String s= "str" void method() { B b = new B(); b.setA(a); b.set(s); } } class B { int a; String s; setA(int a){ // сеттер this.a = a } setS(int s){ this.s = s } } 

You can choose one of the options (or both) and is easy to implement. if you have questions, ask

  • Thanks for the answer! I worked with setters once. Correct me, but in this case I understood it so. In the config created a method for obtaining a token. Added to the text of the question. This method will need to be used in each test? Or you can just add a header and specify the accToken variable? - user328512