Java 我想从一个API响应体中获取值,并使用Cucumber gherkin将其用于另一个API请求

Java 我想从一个API响应体中获取值,并使用Cucumber gherkin将其用于另一个API请求,java,performance-testing,bdd,rest-assured,cucumber-java,Java,Performance Testing,Bdd,Rest Assured,Cucumber Java,场景:验证已发布应用程序的清单 1.给定基本url“baseUrl”和路径“basepath” 2.标题是 3.和查询参数 4.和以下详细信息的应用程序 5.当我执行另一个API时,基本url为“baseUrl”,路径为“basePath” 6.并附加Attributevalue(完整的url为baseUrl+basePath+Attributevalue) 7.和api头 8.和查询参数 9然后是带有200状态代码的成功消息我最近实现了一些非常类似的功能。您可以利用下面的代码并根据需要修改它。

场景:验证已发布应用程序的清单 1.给定基本url“baseUrl”和路径“basepath” 2.标题是 3.和查询参数 4.和以下详细信息的应用程序 5.当我执行另一个API时,基本url为“baseUrl”,路径为“basePath” 6.并附加Attributevalue(完整的url为baseUrl+basePath+Attributevalue) 7.和api头
8.和查询参数

9然后是带有200状态代码的成功消息

我最近实现了一些非常类似的功能。您可以利用下面的代码并根据需要修改它。您可能需要从功能中省略一些步骤。这些步骤作为步骤def实施的一部分包含在下面的代码中

特征

@get
  Scenario: get employee
    
    Given an employee exist in the database with id "2"
   When  user retrieves employee info by id
    Then the status code for get employee is 200
StepDefs

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.And;


import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;


import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;

import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
    public class secondIT {
        
        public static Response response;
        public static ValidatableResponse json;
        public static RequestSpecification request;
        public  static String id;
         public static JsonPath jsonPathEvaluator;
        
        
      
        @Given("an employee exist in the database with id {string}")
        public void an_employee_exists_with_id(String ID){
            secondIT.id=ID;
            RestAssured.baseURI = "http://dummy.www.com/api/v1";
            secondIT.request = RestAssured.given();
        }
        
        @When("user retrieves employee info by id")
        public void user_retrieves_employee_info_by_id(){
            
            secondIT.response = secondIT.request.pathParam("id", secondIT.id).get("/employee/{id}");
            secondIT.jsonPathEvaluator = secondIT.response.jsonPath();
            assertNotNull(response);
            
        }
        
        @Then("the status code for get employee is {int}")
        public void verify_status(int sc){
            System.out.println("status code check..  " );
            secondIT.json = secondIT.response.then().statusCode(sc);
            System.out.println("status code:  " + secondIT.response.getStatusCode());
            assertEquals(sc,secondIT.response.getStatusCode());
        }
}

问题是什么?你试了什么?请提供您的代码