While developing a graphql server it is equally important to develop your integration tests. This can be achieved in java spring boot with the class GraphQLTestTemplate and the graphql-spring-boot-starter-test dependency.
But first, why do we need integration tests?
Having a set of integration tests for your graphql server will allow you to make changes with confidence.
These help ensure that you didn't introduce any bugs or regressions.
You can be more comfortable when performing upgrades in dependencies, especially major bumps.
Ensure that you didn't change the graphql serialized json, this could easily break clients.
Ensure your business functionality actually works as expected.
Ensure downstream services are called (wiremock, grpcmock)
New developer team mates can join the product and see sample requests and responses. They can be more comfortable making changes at an early stage.
Overall, you will go faster.
Approach:
We first create a src/it/java and src/it/resources packages and update our maven build to include those tests. Here we include the following plugins to our POM: maven-resources-plugin, build-helper-maven-plugin, maven-failsafe-plugin and testResources.
We then create a IT test class per resolver.
This test will start an in-memory graphql spring boot server.
We then create graphql queries together with the expected responses and copy/paste them into files in the resources package.
We then use junit5 to create unit tests that will each load a graphql query, submit it to the server using GraphQLTestTemplate. We then can load the matching expected graphql response file from the classpath and assert it equals the response. This can be achieved with library skyscreamer json assertEquals.
For dynamic elements such as a clock (LocalDateTime, Localdate etc), we create the ApplicationContext with a FixedClock. This better than mocking the clock as having the fixedclock as bean will keep our spring application context clean, thus avoid having to restart between tests.
It is extremely important that we do not have any mocks or spy beans in the integration tests. As they will cause the context to dirty, therefore requiring a restart of the server between junit tests and slowing down the build considerably.
See you in the next video!
Cheers!
Philip
Spring Boot GraphQL Java: https://github.com/graphql-java-kicks...
Spring Boot GraphQL GraphQLTestTemplate:
https://github.com/graphql-java-kicks...
grpc mock: https://github.com/Fadelis/grpcmock