代码之家  ›  专栏  ›  技术社区  ›  Saffik

重启Java:如何从setup方法获取标头用户和传递

  •  0
  • Saffik  · 技术社区  · 7 年前

    我有一门课,它有以下内容

    package com.example.misc;
    
    import com.jayway.restassured.RestAssured;
    import com.jayway.restassured.authentication.PreemptiveBasicAuthScheme;
    import org.junit.BeforeClass;
    
    public class QueryEndpoint {
    
        @BeforeClass
        public static void setup() {
            RestAssured.port = 8010;
            PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
            authScheme.setUserName("username123");
            authScheme.setPassword("password123");
            RestAssured.authentication = authScheme;
    
            String basePath;
            basePath = "/api/version1/";
            RestAssured.basePath = basePath;
    
            String baseHost;
            baseHost = "http://localhost";
            RestAssured.baseURI = baseHost;
            }
    
        }
    

    然后在另一节课上,我有一个测试。。。

    package com.example.tests;
    
    import com.example.misc.QueryEndpoint;
    import org.junit.Test;
    
    import static com.jayway.restassured.RestAssured.given;
    import static org.hamcrest.Matchers.equalTo;
    
    public class ApiTest extends QueryEndpoint{
    
    
        @Test
        public void verifyTopLevelURL() {
            given()
                    .auth(). preemptive().basic("username", "password")// THIS LINE DON'T WORK, need to add here something?
                    .contentType("application/json")
                    .when().get("/123456789").then()
                    .body("fruit",equalTo("123456789"))
                    .body("fruit.apple",equalTo(37))
                    .body("fruit.red",equalTo("apple"))
                    .statusCode(200);
        }
    

    我的问题是:如何在方法setup()中使用header+user+pass集,并调用它以用于测试verifyTopLevelURL。

    1 回复  |  直到 7 年前
        1
  •  1
  •   anurag0510    7 年前

    当您从QueryEndpoint类继承ApiTest类时,可以直接使用静态变量方法。以下是代码片段:

    您的QueryEndpoint类:

    package com.example.misc;
    
    import com.jayway.restassured.RestAssured;
    import com.jayway.restassured.authentication.PreemptiveBasicAuthScheme;
    import org.junit.BeforeClass;
    
    public class QueryEndpoint {
        static String userName = "username123";
        static String password = "password123";
        @BeforeClass
        public static void setup() {
            RestAssured.port = 8010;
            PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
            authScheme.setUserName(userName);
            authScheme.setPassword(password);
            RestAssured.authentication = authScheme;
    
            String basePath;
            basePath = "/api/version1/";
            RestAssured.basePath = basePath;
    
            String baseHost;
            baseHost = "http://localhost";
            RestAssured.baseURI = baseHost;
            }
    
        }
    

    您的APIEST类:

    package com.example.tests;
    
    import com.example.misc.QueryEndpoint;
    import org.junit.Test;
    
    import static com.jayway.restassured.RestAssured.given;
    import static org.hamcrest.Matchers.equalTo;
    
    public class ApiTest extends QueryEndpoint{
    
    
        @Test
        public void verifyTopLevelURL() {
            given()
                    .auth(). preemptive().basic(userName, password)
                    .contentType("application/json")
                    .when().get("/123456789").then()
                    .body("fruit",equalTo("123456789"))
                    .body("fruit.apple",equalTo(37))
                    .body("fruit.red",equalTo("apple"))
                    .statusCode(200);
        }
    

    您也可以对标题执行相同的操作。希望这有帮助。