Skip to content

arian/graphql-kotlin-test-dsl

Repository files navigation

Kotlin GraphQL Test DSL for graphql-java

This is a kotlin DSL to easily write (integration) tests for your graphql-java application.

It is inspired by the Spring MockMVC DSL and lets you use JsonPath to quickly retrieve results from the response.

Maven Central Actions

Gradle

testImplementation("com.symbaloo:graphql-kotlin-test-dsl:1.0.9")

Maven

<dependency>
  <groupId>com.symbaloo</groupId>
  <artifactId>graphql-kotlin-test-dsl</artifactId>
  <version>1.0.9</version>
</dependency>

Example

The following code shows an example how to use this test library.

// the graphql-java schema
val schema: GraphQL = createTestSchema()
val result: ExecutionResult = graphQLTest(schema) {
    // define a query
    query(
        """
        |query Init(${"$"}echo: String) {
        |    echo(echo: ${"$"}echo)
        |    hello { hello }
        |}""".trimMargin()
    )
    // add a variable
    variable("echo", "response")
}
    // check for noErrors
    .andExpect { noErrors() }
    // create json context
    .andExpectJson {
        // go into the result with a json path
        path<String>("$.hello.hello") {
            // quick isEqualTo check
            isEqualTo("world")
            // do something with the result
            andDo {
                assertThat(it).isEqualTo("world")
            }
        }
        // combination of `path` and `andDo`
        pathAndDo("$.hello") { it: Map<String, Any> ->
            assertThat(it).contains("hello", "world")
        }

        // combination of `path` and `isEqualTo`
        pathIsEqualTo("$.echo", "response")

        // it can also return values
        val hello = pathAndDo("$.hello") { map: Map<String, Any> ->
            map["hello"]
        }
        assertThat(hello).isEqualTo("world")
    }
    .andReturn()

Link with your Assertion Library

graphQLTest(createTestSchema()) {
    query("{ answer }")
}.andExpectJson {
    assertPath("$.answer").isEqualTo(42)
}

fun GraphQLJsonResultMatcherDsl.assertPath(path: String): Assert<Any?> =
    pathAndDo(path) { it: Any? -> assertThat(it) }

License

MIT License

About

A Kotlin DSL to write tests for your graphql-java schema

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages