I am new in Rest API's. I am trying to run this test function in IntelliJ IDEA to test OMDB API:
@Test
public void shouldNotGetResponseWithoutApiKey(){
Response response = RestAssured.given()
.queryParam("t", "Harry Potter")
.get("http://www.omdbapi.com")
.then()
.statusCode(401)
.extract().response();
assertThat(response.getBody().jsonPath().getString("Error"), Matchers.containsString(errorMessage));
}
Here the logic is: I want this test to give status code 401 since the API Key is not given to the Rest Assured. It's very straight forward. But I receive this error:
io.restassured.internal.http.HttpResponseException: Unauthorized
at io.restassured.internal.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:627)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1268)
...
I searched for the error but I couldn't find a solution. I would be very happy if someone can direct me.
Thanks a lot