简体   繁体   中英

Optimization of rest-assured and gson at rest test with java with cucumber

I got a rest api which returns me this nested response:

"latitude":37.8267,
"longitude":-122.4233,
"timezone":"America/Los_Angeles",
"minutely":{
   "summary":"Clear for the hour.",
   "icon":"clear-day",
   "data":[
       {
         "time":1517431560,
         "precipIntensity":0,
         "precipProbability":0
       },
       {
         "time":1517431620,
         "precipIntensity":0,
         "precipProbability":0
       },
....

So, I need to get minutely weather forecast and put it to the object. I made a HourlyWeather class with getters and setters (not listed):

public class HourlyWeather {
    String time;
    String precipIntensity;
    String precipProbability;

And here are my gherkin steps implemented with java:

    @Given("^rest api$")
        public void restApi() throws Throwable {
            restApiUrl = "https://api.darksky.net/forecast/******"; // my api key
        }

@And("^rest api parameters$")
    public void restApiParameters() throws Throwable {
        restApiUrl = restApiUrl + "/37.8267,-122.4233";
    }

    @When("^I \"([^\"]*)\" rest api execution result$")
    public void iRestApiExecutionResult(String method) throws Throwable {
       RestAssured.baseURI = restApiUrl;
       RequestSpecification httpRequest = RestAssured.given();            response = httpRequest.request(Method.GET);

    }

and here is my question: I'm using rest assured here to get a part of my nested JSON (which I need). And I do a toString conversion here. After that - I'm using GSON to deserialize my string and create a HourlyWeather[] object with all hourly weather json keys data. Is there any way I could avoid this conversion and simplify my code?

     @Then("^I should deserialize result just to know how to do that$")
        public void iShouldDeserializeResultJustToKnowHowToDoThat() throws Throwable {
            // get only part of my nested json
            // I would like ot get this part as an array list of HourlyWeather.class objects, not
            // as ArrayList of HashMaps, so this is String here
            String stringOfRequiredJSON = response.jsonPath().getJsonObject("minutely.data").toString();

            Gson gson = new Gson();
            HourlyWeather[] hourlyWeatherForecast = gson.fromJson(stringOfRequiredJSON, HourlyWeather[].class);
printHourlyWeather(hourlyWeatherForecast);
    }

Thank you!

Yes, if you are willing to switch to another tool, you must evaluate Karate it frees you from having to fight with JSON in Java, which let us all admit - is not the best language to work with JSON.

I'd also like to point to my detailed answer on why trying to use BDD for REST API testing is not the best use of your time: https://stackoverflow.com/a/47799207/143475

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM