在另一个 class 中使用来自扩展 class 的局部变量字符串

Use local variable string from extended class in another class

所以,我在 class.

中有一个 String 类型的局部变量
public class QueryEndpoint {

    @BeforeClass
    public static void setup() {

        String username = "username111";
        String password = "password222";
        StringBuilder authorization = new StringBuilder();
        authorization.append(username).append(":").append(password);
        String authHeader = "Basic " + Base64.getEncoder().encodeToString(authorization.toString().getBytes());
    }

在扩展上述内容的另一个 class 中,我有

package com.example.tests;

import com.jayway.restassured.authentication.PreemptiveBasicAuthScheme;
import com.example.misc.QueryEndpoint;

public class ApiTest extends QueryEndpoint {

    @Test
    public void verifyTopLevelURL() {
        given()
                .header("Authorization", authScheme) // <--HERE
                .contentType("application/json")
                .when().get("/22222222").then()
                .body("fruit",equalTo("22222222"))
                .body("fruit.apple",equalTo(37))
                .body("fruit.banana",equalTo("111"))
                .statusCode(200);
    }
}

如何从 QueryEndpoint 访问 class ApiTest 中的字符串 authHeader?它说无法解析符号 'authScheme'

您可以尝试将 authHeader 设为静态变量

public class QueryEndpoint {

    public static String authHeader;

由于您的局部变量 authHeader 的范围仅在大括号内,因此无法通过任何其他方法访问。

您必须将其作为参数传递给函数或将其设为 class 级别变量。