Search code examples
rest-assuredopenapi-generatorserenity-bdd

Generate Serenity-BDD Rest-Assured tests using OpenAPI Generator


In order to write tests for a given OpenAPI specification (which I do not have under control) I want to generate the RestAssured-Tests decorated by the Serenity-BDD framework using openapi-generator.

My build.gradle.kts file is

dependencies {

    implementation("jakarta.annotation:jakarta.annotation-api:2.1.1")
    implementation("io.rest-assured:rest-assured:5.3.0")
    implementation("com.squareup.okio:okio:3.4.0")
    implementation("io.gsonfire:gson-fire:1.8.5")

    implementation("net.serenity-bdd:serenity-core:4.2.16")
    implementation("net.serenity-bdd:serenity-junit5:4.2.16")
    implementation("net.serenity-bdd:serenity-rest-assured:4.2.16")

    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

openApiGenerate {
    generatorName.set("java")
    library.set("rest-assured")
    templateDir.set("$rootDir/templates/rest-assured-serenity")
    inputSpec.set("$rootDir/open-api.json")
    outputDir.set("$buildDir/generated")
    apiPackage.set("com.example.api")
    modelPackage.set("com.example.model")
    configOptions.set(
        mapOf(
            "useJakartaEe" to "true",
            "interfaceOnly" to "true",
            "useTags" to "true",
            "dateLibrary" to "java8"
        )
    )
}

To do so, I copied the template files from the OpenAPI generator GitHub repository for rest-assured into my local project, added the import for import net.serenitybdd.rest.RestRequests and adapted the method execute to not use the RestAssured.given() but the RestRequests.given() from serenity:

    /**
     * {{httpMethod}} {{{path}}}
     * @param handler handler
     * @param <T> type
     * @return type
     */
    @Override
    public <T> T execute(Function<Response, T> handler) {
        return handler.apply(RestRequests.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(REQ_METHOD, REQ_URI));
    }

It compiles, but when I execute that, I get following exception, because I cannot get the call for expect().spec(...) to work.

Receiver class net.serenitybdd.rest.decorators.ResponseSpecificationDecorated$ByteBuddy$ypwvjVrL does not define or inherit an implementation of the resolved method 'abstract io.restassured.specification.ResponseSpecification spec(io.restassured.specification.ResponseSpecification)' of interface io.restassured.specification.ResponseSpecification.

Do you know which ResponseSpecificationDecorator I could use, because Serenity does not provide one. Or am I doing it completely wrong?


Solution

  • It turned out that following line is enough for Serenity to pick up the OpenAPI generated rest requests:

    @Override
    public <T> T execute(Function<Response, T> handler) {
        return handler.apply(RestRequests.given(reqSpec.build(), respSpec.build()).request(REQ_METHOD, REQ_URI));
    }