Spock is a testing and specification framework with beautiful and highly expressive specification language inspired from JUnit, jMock, RSpec, Groovy, Scala, Vulcans, and other fascinating life forms. In this article, we will show how to test APIs using the Spock framework written in Groovy.
Add dependencies to Gradle build script
We are using gradle to build our project. Gradle is fast, less verbose and overall a better developer experience.
Create a Groovy class and extends Specification
By extending the Specification class, methods written in this class will be automatically detected as test cases.
Set up the request spec
We will use a free json api from JSONPlaceholder. We set up a shared resource here so that we can test multiple endpoints without creating duplicate instances.
Call request and validate json response
Lastly, we call the request and validate that the response is exactly what we expected.