RestAssured-TestNG测试RestfulAPI引发连接超时错误

时间:2019-10-09 20:36:29

标签: testng rest-assured

我正在使用带有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());

    }   

} 

}

3 个答案:

答案 0 :(得分:0)

请这样使用

sheet.Column(columnIndex).Style.Numberformat.Format = "#,##0.00";

答案 1 :(得分:0)

您可以按照以下说明重构协议。我无法复制连接超时错误,因为本地主机上没有主机

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

        }


    }

}

输出

获取未知的主机期望错误:由于您提供了错误的主机名

enter image description here

答案 2 :(得分:0)