我在Eclipse IDE中将RestAssured框架与TestNG,Selenium,Java一起使用。
当我传递参数时写,它会等待几秒钟并抛出连接超时错误。这是天气API。我从浏览器测试了该API。工作正常。从代码中调用时,它将引发连接超时。有什么建议吗?
import org.testng.annotations.BeforeMethod;
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_Weather_GET {
@BeforeMethod
public void beforeMethod() {
System.out.println("before method");
}
@Test
void GetWeatherDetails(){
System.out.println("in Test method before calling baseURI");
//specify the base uri
RestAssured.baseURI = "http://restapi.demoqa.com/utilities/weather/city/";
System.out.println("in Test method after calling baseURI");
System.out.println("in Test method before calling given");
//Request object is being created
RequestSpecification ahttprequest = RestAssured.given();
System.out.println("in Test method after calling given");
System.out.println("passing parameter alameda");
//Response object
Response aresponse = ahttprequest.request(Method.GET, "Bangalore");
System.out.println("after passing parameter alameda");
//print response in console
String aresponseBody = aresponse.getBody().asString();
System.out.println("Response body is : "+aresponseBody);
}
}
before method
in Test method before calling baseURI
in Test method after calling baseURI
in Test method before calling given
in Test method after calling given
passing parameter Bangalore
log4j:WARN No appenders could be found for logger
(org.apache.http.impl.conn.BasicClientConnectionManager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for m
ore info.
FAILED: GetWeatherDetails
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at
org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)