下面的类是具有RequestSpecification和RequestSpecification的超类,并且还包含对代理的调用(端口8888上的Fiddler列表)。
import org.junit.BeforeClass;
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
public class VideoGameDB_MultipleRequestRespSpecificatoin {
public static RequestSpecification videoGameDB_requestSpecification;
public static ResponseSpecification videoGameDB_responseSpecification;
@BeforeClass
public static void setup() {
videoGameDB_requestSpecification = new RequestSpecBuilder().
setBaseUri("http://localhost"). setPort(8080). setBasePath("/app").
addHeader("Control-Type", "application/json"). addHeader("Accept",
"application/json"). build();
RestAssured.requestSpecification = videoGameDB_requestSpecification;
videoGameDB_responseSpecification = new ResponseSpecBuilder().
expectStatusCode(200).
build();
RestAssured.responseSpecification = videoGameDB_responseSpecification;
RestAssured.proxy("localhost", 8888);
}
}
下面的类包含测试方法。
import org.junit.Test;
import static io.restassured.RestAssured.given;
import com.videoGameDB.config.VideoGameDB_MultipleRequestRespSpecificatoin;
public class TestVideoGameDB_MultipleRequestRespSpecificatoin extends VideoGameDB_MultipleRequestRespSpecificatoin {
@Test
public void testCase() {
given().
spec(videoGameDB_requestSpecification).
when().
get("videogames/2").
then().
spec(videoGameDB_responseSpecification);
}
}
在运行虚拟测试请求后,流量不会流过Fiddler。尝试从我的代码中删除“请求和响应规范”,并使用下面的简单代码进行了尝试,并且流量流经Fiddler,我也可以看到请求和响应。不确定使用“请求和响应规范”时发生了什么。
import org.junit.BeforeClass;
import io.restassured.RestAssured;
public class VG_TestConfig {
@BeforeClass
public static void vg_Setup() {
RestAssured.baseURI = "http://localhost";
RestAssured.port = 8080;
RestAssured.basePath = "/app";
RestAssured.proxy("localhost", 8888);
}
}
import org.junit.Test;
import com.videoGameDB.config.VG_TestConfig;
import static io.restassured.RestAssured.given;
public class VG_FirstTestCase extends VG_TestConfig{
@Test
public void vgTestCase() {
given().
when().
get("videogames/2").
then().
statusCode(200);
}
}
在使用请求/响应规范时,任何人都可以建议我应该采取哪些步骤来使流量通过Fiddler。
答案 0 :(得分:0)
在建立请求规范时需要配置代理。您的VideoGameDB_MultipleRequestRespSpecificatoin
类的外观如下:
public class VideoGameDB_MultipleRequestRespSpecificatoin {
public static RequestSpecification videoGameDB_requestSpecification;
public static ResponseSpecification videoGameDB_responseSpecification;
@BeforeClass
public static void setup() {
videoGameDB_requestSpecification = new RequestSpecBuilder()
.setProxy("localhost", 8888) //Configure your proxy here
.setBaseUri("http://localhost")
.setPort(8080)
.setBasePath("/app")
.addHeader("Control-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
RestAssured.requestSpecification = videoGameDB_requestSpecification;
videoGameDB_responseSpecification = new ResponseSpecBuilder().
expectStatusCode(200).
build();
RestAssured.responseSpecification = videoGameDB_responseSpecification;
}
}