Skip to content

[java client, gson] Expose @SerializedName value as constant #21

@jmini

Description

@jmini

Sometimes when you are writing tests for an API (with Rest-Assured in my case) you can not use the fluent API to build request, because you want to create an alternative request (malformed, corner case...)

You can use rest-assured RequestSpecBuilder for that. You then write code like this:

        final String updateBody = "{"
                + "\"id\": \"" + req.getId() + "\", "
                + "\"version\": \"" + req.getVersion() + "\", "
                + "\"name\": \"" + req.getName() + "\", "
                + "\"billingAddressId\": null, "
                + "\"contactAddressId\": null, "
                + "\"metadata\": null}";

        final Account account = createSuperAdminApi().accountsUpdate().simulateQuery(true)
                .reqSpec(b -> b.setBody(updateBody))
                .executeAs(r -> r.thenReturn());

I would be great instead of hard-coding the json member like this, to be able to reference constants:

        final String updateBody = "{"
                + "\""+ Acount.SERIALIZED_NAME_Id +"\": \"" + req.getId() + "\", "
                + "\""+ Acount.SERIALIZED_NAME_Version +"\": \"" + req.getVersion() + "\", "
                + "\""+ Acount.SERIALIZED_NAME_Name +"\": \"" + req.getName() + "\", "
                + "\""+ Acount.SERIALIZED_NAME_BillingAddressId +"\": null, "
                + "\""+ Acount.SERIALIZED_NAME_ContactAddressId +"\": null, "
                + "\"metadata\": null}";

This way, when the API evolves, the Java compiler indicates that something needs to be changed in the test case (the same way that the fluent api is also changed).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions