Search code examples
javaautomationweb-api-testing

Getting java.net.UnknownHostException while executing the basic automation script for Rest Api


I just executed the below script :

import io.restassured.RestAssured;
import static io.restassured.RestAssured.given;
public class Basics {
 public static void main(String[] args) 
 { 
  RestAssured.baseURI="https://maps.googleapis.com";
  given().
  param("location","-33.8670522,151.1957362").
  param("radius","500").
  param("types","food").
  param("key","AIzaSyDXR2YGnRu3-112ttezUsQ-T2zOkb6aafE").
  when().
  get("/maps/api/place/nearbysearch/json").then().assertThat().statusCode(200);  
 }
}

But I am getting an exception while running the script.

Exception in thread "main" java.net.UnknownHostException: maps.googleapis.com
 at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
 at java.net.InetAddress$2.lookupAllHostAddr(Unknown Source)
 at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
 at java.net.InetAddress.getAllByName0(Unknown Source)
 at java.net.InetAddress.getAllByName(Unknown Source)
 at java.net.InetAddress.getAllByName(Unknown Source)
 at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
 at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:263)
 at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:162)
 at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:326)
 at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)

Could someone help me to resolve this issue ?


Solution

  • You probably have a problem with your DNS. If you do nslookup maps.googleapis.com, you should see more or less the following:

    Server:  questgedctvm.com
    Address:  10.197.72.16 
    
    Non-authoritative answer:
    Name:    googleapis.l.google.com
    Addresses:  2a00:1450:4002:805::200a
              216.58.205.74
              216.58.205.106
              216.58.205.138
              216.58.205.170
              216.58.205.202
              216.58.198.10
              216.58.198.42
              172.217.23.106
              216.58.205.42
    Aliases:  maps.googleapis.com
    

    If this is not what you're seeing (the actual IP addresses may differ, but you should get a bunch) then you have a DNS issue that you need to solve. It's not a Java issue.