1

I am using TestNG with RestAssured framework to test RestAPI's.

The moment the execution hits this line, httpRequest.request, it throws connection timeout error

Am I missing anything in that line? It didn't throw any syntax error.

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

    }   

} 

}

bnath002
  • 57
  • 1
  • 7

3 Answers3

0

Please use like this

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 Nair
  • 425
  • 3
  • 11
  • Arun, I modified the code and I am gettting connection timeout error. please see the modified code in the original post – bnath002 Oct 09 '19 at 22:53
0

You can refactor your accord as given below. I'm not able to replicate connection Time out error as Host is not available on my local

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

        }


    }

}

Output

Getting Unknown Host Expection Error: As you have provided the incorrect Host Name

enter image description here

Arpan Saini
  • 4,623
  • 1
  • 42
  • 50
  • Arpan, sorry for the late reply. I am getting the following error: before method FAILED CONFIGURATION: @BeforeMethod beforeMethod java.lang.NoClassDefFoundError: io/restassured/common/mapper/factory/ObjectMapperFactory at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) – bnath002 Oct 16 '19 at 23:20
  • @bnath002: The error are you getting is not related to this class as there is nothing imported from a package of ObjectMapperFactory. The reason for error is the respective dependency is not included in your project. add the respective dependency or Other reason could be There are two different version of the Jar included in your project and the the one jar that is more close to your project has no such method or class in the jar. – Arpan Saini Oct 18 '19 at 22:51
  • @bnath002: 1. The reason for error is the respective dependency is not included in your project. add the respective dependency or 2 second reason could be there are two different versions of the Jar included in your project and the one jar that is more close to your project has no such method or class in the jar. https://stackoverflow.com/questions/6342163/java-lang-classnotfoundexception-org-apache-commons-codec-binary-base64/58399120#58399120 – Arpan Saini Oct 18 '19 at 22:58
-1

Please follow below link. I have explained with the code:-

https://stackoverflow.com/questions/58343693/timeout-error-in-restassured-whereas-the-service-giving-response-in-postman-soap/58392345#58392345

avidCoder
  • 440
  • 2
  • 10
  • 28