下面是一个示例,我展示了如何全局设置基本路径。
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.specification.RequestSpecification;
import static com.jayway.restassured.RestAssured.*;
public class RequestSpecificationTest {
RequestSpecification rspec;
RequestSpecBuilder build;
@BeforeClass
public void requestSpec () {
build = new RequestSpecBuilder();
build.setBaseUri ("https://maps.googleapis.com");
build.setBasePath ("maps/api/place/textsearch/json");
build.addParam ("query", "restaurants in mumbai");
build.addParam ("key", "XYZ");
rspec = build.build ();
}
@Test
public void test01 () {
given()
.spec (rspec)
.when ()
.get ("")
.then ()
.contentType (ContentType.JSON)
.statusCode (200);
}
}
您也可以按照我的教程学习相同的主题:
Using RequestSpecBuilder in Rest Assured ( Code Reuse )