I tried to run the TestRunner.java file code with the help of Hook. Below are the details available. Please have a look and help me out to resolve the issue.
StepDefinition.java File:
package stepDefinations;
import static io.restassured.RestAssured.given;
import static org.junit.Assert.*;
import java.io.IOException;
//import java.util.ArrayList;
//import java.util.List;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
//import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.http.ContentType;
//import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import resources.APIResource;
//import pojo_serialization.Location;
//import pojo_serialization.googleResponse;
import resources.TestDataBuild;
import resources.Utils;
public class StepDefination extends Utils{
RequestSpecification res;
ResponseSpecification resSpec;
Response response;
TestDataBuild td=new TestDataBuild();
static String place_id;
@Given("Add Place payload {string} {string} {string}")
public void add_place_payload(String name, String language, String address) throws IOException {
res=given().spec(requestSpecification()).body(td.AddPlacePayload(name, language, address));
}
@When("user calls {string} with {string} http request")
public void user_calls_with_http_request(String resource, String method) {
APIResource resourceAPI=APIResource.valueOf(resource);
resSpec=new ResponseSpecBuilder().expectStatusCode(200).expectContentType(ContentType.JSON).build();
response=res.when().post("maps/api/place/add/json")
.then().spec(resSpec).extract().response();
if (method.equalsIgnoreCase("POST"))
response=res.when().post(resourceAPI.getResource());
else if (method.equalsIgnoreCase("GET"))
response= res.when().get(resourceAPI.getResource());
}
@Then("API call got success with status {int}")
public void api_call_got_success_with_status(Integer int1) {
assertEquals(response.getStatusCode(),200);
//throw new io.cucumber.java.PendingException();
}
@Then("{string} in response body is {string}")
public void in_response_body_is(String keyValue, String expectedValue) {
assertEquals(getJsonPath(response,keyValue),expectedValue);
}
@Then("verify place_id created maps to {string} using {string}")
public void verify_place_id_created_maps_to_using(String expectedName, String resource) throws IOException {
place_id=getJsonPath(response, "place_id");
res=given().spec(requestSpecification()).queryParam("place_id",place_id);
user_calls_with_http_request(resource, "GET");
String Actualname=getJsonPath(response, "name");
assertEquals(Actualname,expectedName);
}
@Given("Delete place payload")
public void delete_place_payload() throws IOException {
res= given().spec(requestSpecification())
.body(td.deleteplacePayload(place_id));
user_calls_with_http_request("deletePlaceAPI", "POST");
}
}
Hook.java file
package stepDefinations;
import java.io.IOException;
import io.cucumber.java.Before;
public class Hooks {
@Before("@DeletePlace")
public void beforeScenario() throws IOException {
StepDefination sd= new StepDefination();
if(StepDefination.place_id==null) {
sd.add_place_payload("White house","English UK", "London");
sd.user_calls_with_http_request("AddPlaceAPI", "POST");
sd.verify_place_id_created_maps_to_using("White house", "getPlaceAPI");
}
}
}
TestRunner.java file:
package cucumber.Options;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/java/features/placeValidation.feature",
glue = {"stepDefinations"}, plugin={"pretty"}, tags="@DeletePlace")
public class TestRunner {
}
The feature File:
Feature: Validating Place API
@AddPlace
Scenario Outline: Verify the place is being successfully added using AddPlaceAPI
Given Add Place payload "<name>" "<language>" "<address>"
When user calls "AddPlaceAPI" with "POST" http request
Then API call got success with status 200
And "status" in response body is "OK"
And "scope" in response body is "APP"
And verify place_id created maps to "<name>" using "getPlaceAPI"
Examples:
|name |language|address|
|Ansuman |English |India|
#|Pizza house|French|USA|
@DeletePlace
Scenario: Verify if deletePlaceAPI functionality is working.
Given Delete place payload
When user calls "deletePlaceAPI" with "POST" http request
Then API call got success with status 200
And "status" in response body is "OK"
The resource.java file:
package resources;
public enum APIResource {
AddPlaceAPI("maps/api/place/add/json"),
getPlaceAPI("maps/api/place/get/json"),
deletePlaceAPI("maps/api/place/delete/json");
public String resource;
APIResource(String resource) {
this.resource=resource;
}
public String getResource()
{
return resource;
}
}
I am getting the below Assertion error. I am unable to identify where it went wrong.
@DeletePlace
Scenario: Verify if deletePlaceAPI functionality is working. [90m# src/test/java/features/placeValidation.feature:19[0m
[32mGiven [0m[32mDelete place payload[0m [90m# stepDefinations.StepDefination.delete_place_payload()[0m
[32mWhen [0m[32muser calls [0m[32m[1m"deletePlaceAPI"[0m[32m with [0m[32m[1m"POST"[0m[32m http request[0m [90m# stepDefinations.StepDefination.user_calls_with_http_request(java.lang.String,java.lang.String)[0m
[31mThen [0m[31mAPI call got success with status [0m[31m[1m200[0m [90m# stepDefinations.StepDefination.api_call_got_success_with_status(java.lang.Integer)[0m
[**31mjava.lang.AssertionError: expected:<404> but was:<200>**
at org.junit.Assert.fail(Assert.java:89)
at org.junit.Assert.failNotEquals(Assert.java:835)
at org.junit.Assert.assertEquals(Assert.java:647)
at org.junit.Assert.assertEquals(Assert.java:633)
at stepDefinations.StepDefination.api_call_got_success_with_status(StepDefination.java:55)
at ✽.API call got success with status 200(file:///C:/Users/ansuman.pati/eclipse-workspace/APIFramework/src/test/java/features/placeValidation.feature:22)
[0m
[36mAnd [0m[36m[0m[36m[1m"status"[0m[36m in response body is [0m[36m[1m"OK"[0m [90m# stepDefinations.StepDefination.in_response_body_is(java.lang.String,java.lang.String)[0m