Introduction to Spock: A Unit Testing Framework

Reading Time: 4 minutes

In this article, we will be talking about the Spock framework. Testing components has been an essential part of application development, be it unit testing or integration testing. Tested components are reliable and give us confidence about our product in any way. So, if testing components is easy, it will automatically help us write better test cases in less time which will not only save some cost but will increase the productivity by many times. Now, we know the importance of testing and why do we need a testing framework that will make writing test cases easier for us?

Introduction to Spock
Spock is a BDD-style developer testing and specification framework for Java and Groovy applications. Spock combines unit testing, mocking stubbing and business driven development all into one framework so you don’t have to download/include additional libraries in your build. Everything is inbuilt in Spock framework, it was inspired by JUnit framework only and JUnit is also used under the hood. We will write the code in Java but we will test the code in Groovy.

Look at the figure below for better understanding of Spock.

Features of Spock

  1. Highly expressive – Test cases can be expressed in terms of given, when and then blocks

2. Compatible with most IDEs – It is compatible with all the leading IDEs in the market

3. Compatible with build tools – Compatible with Maven as well as Gradle

4. Compatible with Continuous integration servers – The JUnit runner makes it compatible with all the continuous integration servers out there.

Supported Java versions

Spock is supported for Java version 7, and 8 (with Groovy 2.5 Java 9+ are supported as well). In this article, we would be using the Maven build tool along with Java8 for the demo purpose.

Why Spock framework?

You must be wondering why do we even need the Spock framework when we already have JUnit to write test cases. There are various reasons for that, let’s take a look at them one by one.

Following are the reasons to go for Spock framework

  1. Highly Expressive Specification Language.
  2. Compatibility with leading IDEs
  3. Test cases are written in Groovy which is pretty much similar to Java and also a JVM based language.
  4. One framework for all types of test cases, no other libraries are required to write test cases.
  5. Expressive than the typical Junit
  6. Easy data data driven testing which was very verbose in Junit

What is a Specification in Groovy?

A specification is represented as a Groovy class that extends from spock.lang.Specification. The name of a specification usually relates to the system or system operation described by the specification.

Class Specification contains a number of useful methods for writing specifications. Furthermore it instructs JUnit to run specification with Sputnik, Spock’s JUnit runner. Thanks to Sputnik, Spock specifications can be run by most modern Java IDEs and build tools.

Basic Structure of a Spock Specification


class MyFirstApplicationSpec extends Specification {

// fields - place to store objects
// fixture methods - are responsible of configuring the system under specification (SUS) before feature methods are invoked and cleaning up of the system under specification after feature methods have been invoked.
// feature methods - specify the expected behavior of the system under specification.
// helper methods - are methods that are used by the other methods found from the specification class.
}


Test classes in Groovy are also known as Specification or just Spec. So whenever you write a test file it should end with suffix Spec/Specification and same should be communicated to the build using surefire plugin to execute test cases seamlessly


Structure of a Spock Test class or Specification

@Test
public void givenTwoAndTwo_whenAdding_thenResultIsFour() {
   // Given
   int first = 2;
   int second = 4;
 
   // When
   int result = 2 + 2;
 
   // Then
   assertTrue(result == 4)
}

As you can see in the example above, the test case looks so expressive and represents the behavioral design at first where given, when and then are the building blocks for a test case and makes the test case absolutely expressive in terms of understanding.

About Specifications
1. All the Spock Specification would extend the spock.lang.Specification class.

A Spock specification can have instance fields, fixture methods, feature methods, and helper methods

3. We can initialize and clean up the system under specification by using fixture methods.

Note – Spock unit tests will need to be placed in the src/test/groovy/ folder, while JUnit tests (if any) are still in src/test/java/ as per Maven guidelines.

That’s pretty much it from the article, full code can be found on the GitHub repository, feel free to fork it and start practicing the examples. This was just the introduction of Spock framework, stay tuned for more blogs on Spock where we will talk about Mocking and other complex examples. If you have any feedback or queries, please do let me know in the comments. Also, if you liked the article, please give me a thumbs up and I will keep writing blogs like this for you in the future as well. Keep reading and Keep coding.

References


Knoldus-blog-footer-image

Written by 

Deepak is a Software Consultant having experince of more than 5 years . He is very enthusiastic towards his work and is a good team player. He has sound knowledge of different technologies which include Java, C++, C, HTML, CSS, Javascript, C# always keen to learn new technologies.

Leave a Reply