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.
testImplementation("com.symbaloo:graphql-kotlin-test-dsl:1.0.9")
<dependency>
<groupId>com.symbaloo</groupId>
<artifactId>graphql-kotlin-test-dsl</artifactId>
<version>1.0.9</version>
</dependency>
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()
graphQLTest(createTestSchema()) {
query("{ answer }")
}.andExpectJson {
assertPath("$.answer").isEqualTo(42)
}
fun GraphQLJsonResultMatcherDsl.assertPath(path: String): Assert<Any?> =
pathAndDo(path) { it: Any? -> assertThat(it) }
MIT License