我有一个具有以下内容的课程
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 set并调用它以在我的测试verifyTopLevelURL中使用。
答案 0 :(得分:1)
您可以直接使用静态变量方法,因为您从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;
}
}
您的ApiTest课程:
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);
}
你也可以用标题做同样的事情。希望这有帮助。