Skip to content

Reduce duplicated code in tests #578

@amatosg

Description

@amatosg

Is your feature request related to a problem? Please describe.
Not really, just an improvement

Describe the solution you'd like
Instead of having each test call

given()
.auth()
.preemptive()
.oauth2(adminToken)
.accept(APPLICATION_JSON)
.when()
.get("/api/payment-terms")
.then()
.statusCode(OK.getStatusCode())
.contentType(APPLICATION_JSON)
.extract()
.as(LIST_OF_ENTITY_TYPE)
.size();

we could have a private method:

private int getSizeBeforeCreate() {
    return given()
            .auth()
            .preemptive()
            .oauth2(adminToken)
            .accept(APPLICATION_JSON)
            .when()
            .get("/api/entity")
            .then()
            .statusCode(OK.getStatusCode())
            .contentType(APPLICATION_JSON)
            .extract()
            .as(LIST_OF_ENTITY_TYPE)
            .size();
}

and in each test just call var databaseSizeBeforeCreate = getSizeBeforeCreate();

The same applies for persisting the entity;

private ValidatableResponse persistEntity() {
    return given()
            .auth()
            .preemptive()
            .oauth2(adminToken)
            .contentType(APPLICATION_JSON)
            .accept(APPLICATION_JSON)
            .body(entityDTO)
            .when()
            .post("/api/entity")
            .then();
}

then call it and test what you need (if it should fail, success, etc), like

// Create the entity
entityDTO = persistEntity()
    .statusCode(CREATED.getStatusCode())
    .extract()
    .as(ENTITY_TYPE);

// Bad request for whatever reason
entityDTO.id = 1L; // create with an existing ID
persistEntity().statusCode(BAD_REQUEST.getStatusCode());

Maybe even have the endpoint as parameter in case future extensions test custom implementation.

Describe alternatives you've considered
doing it manually

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions