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

时间:2018-02-02 13:46:30

标签: java http inheritance rest-assured

所以,我在类中有一个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());
    }

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

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访问ApiTest类中的String authHeader?它说不能解决符号&#39; authScheme&#39;

1 个答案:

答案 0 :(得分:-1)

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

public class QueryEndpoint {

    public static String authHeader;

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

您必须将其作为参数传递给函数或使其成为类级变量。