【问题标题】:RestAssured - TestNG testing RestfulAPI throws connection timeout errorRestAssured - TestNG 测试 RestfulAPI 引发连接超时错误
【发布时间】:2020-02-07 06:00:36
【问题描述】:

我正在使用带有 RestAssured 框架的 TestNG 来测试 RestAPI。

执行到这一行的那一刻,httpRequest.request,它会抛出连接超时错误

我在那一行有什么遗漏吗?它没有抛出任何语法错误。

   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());

    }   

} 

}

【问题讨论】:

    标签: testng rest-assured


    【解决方案1】:

    请这样使用

    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");
    

    【讨论】:

    • Arun,我修改了代码,但出现连接超时错误。请看原帖中修改后的代码
    【解决方案2】:

    你可以重构你的协议,如下所示。我无法复制连接超时错误,因为我的本地主机不可用

    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());
    
            }
    
    
        }
    
    }
    

    输出

    出现未知主机期望错误:因为您提供了不正确的主机名

    【讨论】:

    • Arpan,抱歉回复晚了。我收到以下错误:在方法失败配置之前:@BeforeMethod beforeMethod java.lang.NoClassDefFoundError: io/restassured/common/mapper/factory/ObjectMapperFactory at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader .defineClass(未知来源)
    • @bnath002:您得到的错误与此类无关,因为没有从 ObjectMapperFactory 的包中导入任何内容。错误的原因是相应的依赖项未包含在您的项目中。添加相应的依赖项或其他原因可能是您的项目中包含两个不同版本的 Jar,并且与您的项目更接近的一个 jar 在 jar 中没有此类方法或类。
    • @bnath002: 1. 错误的原因是您的项目中没有包含相应的依赖项。添加相应的依赖项或 2 秒原因可能是您的项目中包含两个不同版本的 Jar,并且与您的项目更接近的一个 jar 在 jar 中没有此类方法或类。 stackoverflow.com/questions/6342163/…
    【解决方案3】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 2021-06-18
    • 2011-11-16
    • 1970-01-01
    相关资源
    最近更新 更多