Skip to content

Kotlin Spring RestAssured sample

This sample demonstrates the template generator with Kotlin, including custom Mustache templates for complete control over generated test code.

View on GitHub

Overview

Property Value
Module samples:kotlin-spring-rest-assured
Generator template
Template set restassured-kotlin + custom templates
Output location build/generated/openapi-tests/ and src/test/kotlin/

What it demonstrates

  • Using the template generator with built-in restassured-kotlin templates
  • Custom Mustache templates for full control over generated code
  • Three generation tasks with different configurations
  • RestAssured Kotlin extensions
  • Integration with Spring Boot test context

Plugin configuration

Default generation (to build directory)

openApiTestGenerator {
    specFile.set(file("${rootDir}/samples/openapi.yaml").toURI().toString())
    outputDir.set(layout.buildDirectory.dir("generated/openapi-tests/art/galushko/kotlin/spring/rest/assured"))
    generator.set("template")

    generatorOptions.putAll(
        mapOf(
            "templateSet" to "restassured-kotlin",
            "templateVariables" to mapOf(
                "package" to "art.galushko.kotlin.spring.rest.assured.generatedtests",
                "baseUrl" to "http://localhost:8080/v1",
                "springBootTest" to "true",
            ),
        )
    )

    testGenerationSettings {
        ignoreTestCases.putAll(mapOf(
            "/orders" to mapOf("GET" to listOf("Invalid Query page parameter: Integer Breaking"))
        ))
        validSecurityValues.putAll(mapOf("ApiKeyAuth" to "test-api-key-123"))
        overrideBasicTestData.putAll(mapOf("invalidApiKey" to "unrealistic_key"))
        maxSchemaDepth.set(15)
        errorMode.set(ErrorMode.FAIL_FAST)
    }
}

Generation to source directory (YAML config)

tasks.register<OpenApiTestGeneratorTask>("generateOpenApiTestsToSrc") {
    configFile.set("open-api-test-generation-config.yaml")
}

tasks.named("compileTestKotlin") { dependsOn("generateOpenApiTestsToSrc") }

Custom templates generation

tasks.register<OpenApiTestGeneratorTask>("generateOpenApiTestsCustomTemplate") {
    specFile.set(file("${rootDir}/samples/openapi.yaml").toURI().toString())
    outputDir.set(layout.projectDirectory.dir("src/test/kotlin/art/galushko/kotlin/spring/rest/assured/custom"))
    generator.set("template")

    generatorOptions.putAll(
        mapOf(
            "templateSet" to "restassured-kotlin",
            "customTemplateDir" to "samples/kotlin-spring-rest-assured/templates",
            "classTemplatePath" to "class.mustache",
            "outputFileExtension" to "kt",
            "templateVariables" to mapOf(
                "package" to "art.galushko.kotlin.spring.rest.assured.custom",
                "baseUrl" to "http://localhost:8080/v1",
                "springBootTest" to "true",
            ),
        )
    )

    testGenerationSettings {
        validSecurityValues.putAll(mapOf("ApiKeyAuth" to "test-api-key-123"))
    }
}

tasks.named("compileTestKotlin") { dependsOn("generateOpenApiTestsCustomTemplate") }

Custom templates

The sample includes custom Mustache templates in the templates/ directory:

templates/
├── class.mustache     # Main class template
└── method.mustache    # Test method partial (included via {{> method}})

Example class.mustache

{{#customVariables.package}}
package {{customVariables.package}}
{{/customVariables.package}}

import io.restassured.module.kotlin.extensions.*
import org.junit.jupiter.api.Test
{{#customVariables.springBootTest}}
import org.springframework.boot.test.context.SpringBootTest
{{/customVariables.springBootTest}}

{{#customVariables.springBootTest}}
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
{{/customVariables.springBootTest}}
class {{className}} {

    companion object {
        private const val BASE_URL = "{{customVariables.baseUrl}}"
    }

{{#methods}}
{{> method}}
{{/methods}}
}

Example method.mustache

    @Test
    fun `{{testCaseName}}`() {
        Given {
            baseUri(BASE_URL)
{{#headers}}
            header("{{key}}", "{{value}}")
{{/headers}}
{{#queryParams}}
            queryParam("{{key}}", "{{value}}")
{{/queryParams}}
{{#requestBody}}
            contentType("application/json")
            body("""{{requestBody.rawBody}}""")
{{/requestBody}}
        } When {
            {{httpMethod}}("{{path}}")
        } Then {
            statusCode({{expectedStatusCode}})
        }
    }

Generated test example

With custom templates, the generated Kotlin test uses RestAssured Kotlin DSL:

package art.galushko.kotlin.spring.rest.assured.custom

import io.restassured.module.kotlin.extensions.*
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class ListUsersTest {

    companion object {
        private const val BASE_URL = "http://localhost:8080/v1"
    }

    @Test
    fun `Valid request`() {
        Given {
            baseUri(BASE_URL)
            header("X-API-Key", "test-api-key-123")
        } When {
            get("/users")
        } Then {
            statusCode(200)
        }
    }

    @Test
    fun `Missing security: AllSecurityMissed`() {
        Given {
            baseUri(BASE_URL)
        } When {
            get("/users")
        } Then {
            statusCode(401)
        }
    }
}

Running the sample

# Generate tests to build directory (built-in templates)
./gradlew :samples:kotlin-spring-rest-assured:generateOpenApiTests

# Generate tests to src/test/kotlin (YAML config)
./gradlew :samples:kotlin-spring-rest-assured:generateOpenApiTestsToSrc

# Generate tests with custom templates
./gradlew :samples:kotlin-spring-rest-assured:generateOpenApiTestsCustomTemplate

# Compile and run all tests
./gradlew :samples:kotlin-spring-rest-assured:test

Dependencies

dependencies {
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")

    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("io.rest-assured:rest-assured:5.5.0")
    testImplementation("io.rest-assured:kotlin-extensions:5.5.0")
}

The kotlin-extensions dependency provides the idiomatic Kotlin DSL (Given, When, Then blocks).

Template customization options

Option Description
templateSet Base template set (restassured-kotlin)
customTemplateDir Directory containing custom templates
classTemplatePath Path to main class template (relative to customTemplateDir)
outputFileExtension File extension for output (.kt)

When to use custom templates

Custom templates are useful when you need:

  • Different test framework (TestNG, Kotest, etc.)
  • Custom assertions or matchers
  • Additional test setup/teardown
  • Different code style or conventions
  • Integration with custom test utilities