Testing Your API Assertions with the Spock Framework
Spock is a testing framework that uses the Java and Groovy programming languages. It’s a comprehensive testing framework, covering everything you need for unit testing, BDD and Mocks. Of course, you can test your APIs with Spock.
Created in 2008 by Peter Niederwieser from Gradle who was later joined by Luke Daley (also from Gradle), the first release of Spock was in 2015. Since then, it has been used as the default test framework in Grails, as well as being used internally by Gradle, Groovy, MongoDb, Tapestry, Netflix and more.
Why Use Spock?
Spock leverages the benefits of existing Java frameworks: JUnit, TestNG, JBehave, Cucumber, Mockito, JMock and EasyMock. Spock’s main strength is that it has only one dependency. Therefore, users don’t need different dependencies for each of these tools. Instead, they have one for Spock.
In addition, the LOC (lines of code) produced by Spock are much shorter, which makes Spock tests much more readable and maintainable.
Let’s look at an example. Given we have a Java class that check files extensions:
The body of the method is not important for us, so for saving space I exchanged it with “...”.
Now, let’s cover this class with a Spock test.
This Spock class is 10 LOC and results in 72 test scenarios. Want to try doing the same in JUnit? Go ahead, but if you work with JUnit, you know that the same test would be much longer, roughly up to 3 times more!
Another big pro for Spock is that it uses the JUnit runner. This means that if you’ve already been running your tests with it, you can keep doing so, and you don’t need to learn how to use a new runner! In addition, this makes your tests compatible with all existing JUnit tools.
Last but not least, Spock is written in Java and has a Groovy front-end (same as Gradle). This means that most of Java’s functionalities can be used as is, in Spock and Groovy tests or projects.
However, to use Spock you have to learn a new language, Groovy. That being said, Groovy is an easy to learn language. Also, it’s possible to use Java-like code for the first times you work with Groovy, until you become more professional at it.
Now let’s look at how you can test your API assertions with Spock.
1. Add the required dependencies to our build.gradle file:
In the plugins section -
In the dependencies section -
That’s it! Spock is now enabled for our project. If you are using IntelliJ IDEA you will see that you now have two newly generated Groovy folders, in “main” and “test” (see picture):
2. Put your test Groovy class in the src/test/groovy folder on GitHub and name it ArrivalControllerTest. As you can guess, it will test the ArrivalController java class.
Let’s take a look at what we have inside that test class:
Now let’s understand what is going on inside this test.
This part of code is responsible for:
- Ramping up a Spring Context with a defined port and the main class BlazeMeterApi.class
- Making sure this file will work only on test profiles
This is a simple REST client for interacting with API endpoints.
This part is the actual test. In this test scenario we are calling the VERSION + ARRIVAL + 'all' endpoint, and then asserting the status and part of the response body as a JSON with items.
- The method def “Check Arrivals controller”() is a fully qualified String message. This will be visible in execution and make it easier to find out what is going on in the test, step by step.
- When - BDD Gherkin language. In this part of test we are calling the API endpoint.
- Then/And - BDD Gherkin language. This is the part that performs the assertions. In this case: assert response.data.id == 1 and assert response.data.city == 'Buenos Aires'.
Please note that there are no assertions like in JUnit , rather there is only one assert that will make sure the equation is valid.
4. Finally let’s look at the execution results when everything is going well. On the left side, you can see the name that we used in the Spock test as a method name.
Now, let’s look at a case when the test fails, so we can see the power of Spock assert.
So I changed the id to 2 , added a whitespace in the Buenos Aires and changed 7 to 8. After the execution, I got this error message:
As you can see, in other cases you would only have a message that the Assertion failed. However, Spock provides you with everything you need to debug and fix the test, even without debugging the test/application itself. We can immediately see that the id of the item with index 0 can not be 2 , because Spock provided us with the list of items as proof.
This is very important, because if you are working in big enterprise projects, you know that ramping up an environment is sometimes very expensive, and even though the test and fix only require 5-10 minutes, the whole process will take hours.
That’s it! You now know how to check your API assertions with Spock. Get your API testing started with BlazeMeter, now.
You might also find these useful:
Interested in writing for our Blog? Send us a pitch!