restasured-TestNG测试RestfulAPI抛出连接超时错误
我使用TestNG和RestAssured框架来测试RestAPI 当执行到达这一行httpRequest.request时,它抛出连接超时错误 我在那一行遗漏了什么吗?它没有抛出任何语法错误restasured-TestNG测试RestfulAPI抛出连接超时错误,testng,rest-assured,Testng,Rest Assured,我使用TestNG和RestAssured框架来测试RestAPI 当执行到达这一行httpRequest.request时,它抛出连接超时错误 我在那一行遗漏了什么吗?它没有抛出任何语法错误 import org.testng.annotations.BeforeMethod; import static io.restassured.RestAssured.ntlm; import static io.restassured.RestAssured.basic;
import org.testng.annotations.BeforeMethod;
import static io.restassured.RestAssured.ntlm;
import static io.restassured.RestAssured.basic;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
public class RestApi_Incidents {
@BeforeMethod
public void beforeMethod() {
System.out.println("before method");
}
@Test
void GetIncidentAPI(){
try{
RestAssured.baseURI = "https://xxx/api/data/v8.2";
RestAssured.port = 80;
RestAssured.basePath = "/incident";
RestAssured.authentication = basic("userid", "pwd!");
//RestAssured.authentication = ntlm("uid", "pws!", null, "uat");
RequestSpecification httpRequest = RestAssured.given();
Response response =httpRequest.get();
}
catch (Exception ex){
System.out.println(ex.toString());
}
}
}请像这样使用
RestAssured.baseURI = "https://xxx/api/data/v8.2/";
RestAssured.port = 80;
RestAssured.basePath = "/incidents/resource/HealthCheckApp";
RestAssured.authentication = basic("username", "password");
RestAssured.authentication = ntlm("myuserid", null, null, "uat");
RequestSpecification httpRequest = RestAssured.given();
Response response =httpRequest.get("/DetailsView");
您可以按照下面的说明重构accord。我无法复制连接超时错误,因为主机在我的本地计算机上不可用
package api.application.zippopotam;
import io.restassured.authentication.AuthenticationScheme;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.internal.http.HTTPBuilder;
import org.testng.annotations.BeforeMethod;
import static io.restassured.RestAssured.ntlm;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
public class RestApi_Incidents {
public static RequestSpecification requestSpecification ;
@BeforeMethod
public void beforeMethod() {
System.out.println("before method");
requestSpecification = new RequestSpecBuilder().
setBaseUri("https://xxx/api/data/v8.2/incidents").
setRelaxedHTTPSValidation().
setBasePath("HealthCheckApp/DetailsView").build()
.auth().basic("userid", "pwd!");
}
@Test
void GetIncidentAPI(){
try{
Response aresponse = RestAssured.
given().
spec(requestSpecification).
when().
get().
then().
extract().
response();
System.out.println("before getBody");
String aresponseBody = aresponse.getBody().asString();
System.out.println("response is " + aresponseBody);
}
catch (Exception ex){
System.out.println(ex.toString());
}
}
}
输出
获取未知主机预期错误:,因为您提供了不正确的主机名
请点击下面的链接。我已经用代码解释了:-
Arun,我修改了代码,发现连接超时错误。请在原邮包中查看修改后的代码,很抱歉回复太晚。我收到以下错误:before方法配置失败:@BeforeMethod BeforeMethod java.lang.NoClassDefFoundError:io/restassured/common/mapper/factory/ObjectMapperFactory位于java.lang.ClassLoader.defineClass1(本机方法)位于java.lang.ClassLoader.defineClass(未知源)@bnath002:您得到的错误与此类无关,因为没有从ObjectMapperFactory包导入任何内容。出现错误的原因是项目中未包含相应的依赖项。添加相应的依赖项或其他原因可能是您的项目中包含两个不同版本的Jar,而更接近您的项目的Jar中没有此类方法或类。@bnath002:1。出现错误的原因是项目中未包含相应的依赖项。添加相应的依赖项或第二个原因可能是您的项目中包含两个不同版本的Jar,而更接近您的项目的一个Jar在Jar中没有这样的方法或类。