Java 重新启动时出现超时错误,而在postman/soapUI中提供响应的服务
此代码给出超时错误,而服务在postman/soapUI中给出响应 我正在尝试自动化rest服务。服务工作正常,但在自动启动时,会出现超时错误Java 重新启动时出现超时错误,而在postman/soapUI中提供响应的服务,java,rest-assured,rest-assured-jsonpath,Java,Rest Assured,Rest Assured Jsonpath,此代码给出超时错误,而服务在postman/soapUI中给出响应 我正在尝试自动化rest服务。服务工作正常,但在自动启动时,会出现超时错误 import org.testng.annotations.Test; import static io.restassured.RestAssured.given; import io.restassured.RestAssured; import io.restassured.response.Response; public class Add
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import io.restassured.RestAssured;
import io.restassured.response.Response;
public class AddUsers {
@Test
public void addUsers()
{
RestAssured.baseURI = "http://reqres.in";
given().header("Content-Type","application/json").body("{\r\n" +
" \"name\": \"Mallik\",\r\n" +
" \"job\": \"TestLead\"\r\n" +
"}").when().post("/api/users");
}
}
您需要使用https。我得到了正确的输出
RestAssured.baseURI = "https://reqres.in";
Response resp = given().header("Content-Type", "application/json")
.body("{\n" + " \"name\": \"Mallik\",\n" + " \"job\": \"leader\"\n" + "}").when()
.post("/api/users");
System.out.println(resp.getStatusCode());
System.out.println(resp.asString());
您需要使用https。我得到了正确的输出
RestAssured.baseURI = "https://reqres.in";
Response resp = given().header("Content-Type", "application/json")
.body("{\n" + " \"name\": \"Mallik\",\n" + " \"job\": \"leader\"\n" + "}").when()
.post("/api/users");
System.out.println(resp.getStatusCode());
System.out.println(resp.asString());
尝试使用下面的方法。我为addUsers和getUsers添加了代码。我建议您为要通过POST端点的主体创建POJO类
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import net.serenitybdd.rest.SerenityRest;
public class Test11 {
static Response response;
@Test
public void addUsers() {
RestAssured.baseURI = "http://reqres.in";
response = SerenityRest.given().urlEncodingEnabled(false).relaxedHTTPSValidation().contentType(ContentType.JSON)
.log().all()
.when().body("{\r\n" +
" \"name\": \"Mallik\",\r\n" +
" \"job\": \"TestLead\"\r\n" +
"}")
.post("/api/users")
.then().log().all().extract().response();
}
@Test
public void getUsers() {
RestAssured.baseURI = "http://reqres.in";
response = SerenityRest.given().urlEncodingEnabled(false).relaxedHTTPSValidation().contentType(ContentType.JSON)
.log().all()
.when()
.get("/api/users?id=3")
.then().log().all().extract().response();
}
}
如果这解决了您的问题,请告诉我。尝试使用以下选项。我为addUsers和getUsers添加了代码。我建议您为要通过POST端点的主体创建POJO类
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import net.serenitybdd.rest.SerenityRest;
public class Test11 {
static Response response;
@Test
public void addUsers() {
RestAssured.baseURI = "http://reqres.in";
response = SerenityRest.given().urlEncodingEnabled(false).relaxedHTTPSValidation().contentType(ContentType.JSON)
.log().all()
.when().body("{\r\n" +
" \"name\": \"Mallik\",\r\n" +
" \"job\": \"TestLead\"\r\n" +
"}")
.post("/api/users")
.then().log().all().extract().response();
}
@Test
public void getUsers() {
RestAssured.baseURI = "http://reqres.in";
response = SerenityRest.given().urlEncodingEnabled(false).relaxedHTTPSValidation().contentType(ContentType.JSON)
.log().all()
.when()
.get("/api/users?id=3")
.then().log().all().extract().response();
}
}
如果这解决了您的问题,请告诉我。我也遇到了同样的问题,在配置代理主机名和端口后,问题得到了解决,如下所示:
RestAssured.given(restAssuredSpecBuilder.build()).proxy("http.proxy.domain.com",portNumber)
我也遇到了同样的问题,在配置代理主机名和端口后,问题得到了解决,如下所示:
RestAssured.given(restAssuredSpecBuilder.build()).proxy("http.proxy.domain.com",portNumber)