Why to use Spock

      0 min read      ·

From Spock documentation:

Spock is a testing and specification framework for Java and Groovy applications. What makes it stand out from the crowd is its beautiful and highly expressive specification language.

Spock in an amazing testing framework, which I believe is a better replacement for JUnit library. Using Spock will make your tests not only easier to write, but what is more important, it will make them easier to read and understand their results.

What could be the biggest challenges to switch from JUnit to Spock:

  1. Spock is using Groovy (not Java) so you need to learn new language.
  2. Spock is not JUnit so it is not compatible with other software (i.e. IDE, build tools, continuous integration tools).

Fortunatelly, the above statements are false!

  1. It is true that Spock is using Groovy, but Java code is (in like 99%) valid Groovy code, so Groovy has extremely low entry point for Java developers. Just start writing your tests using Java-like syntax and you can move to use Groovy features gradually.
  2. Actually, Spock is like JUnit when it comes to running tests. It’s because Spock is using JUnit runner which is supported by most (if not all) Java IDEs, continuous integration and build tools (Maven/Gradle). So… just write your tests in Spock, but run them the same way as JUnit tests.

OK, so it’s fine… now you know why not to avoid Spock, but why to invest your time (even if it’s not much) in learning new thing when JUnit works fine?

Why to use Spock:

 

I could add more items on this list, but instead here are a few examples of the Spock greatness:

Parameterised test example:

def "maximum of two numbers"(int a, int b, int c) {
        expect:
        Math.max(a, b) == c

        where:
        a | b | c
        1 | 3 | 3
        7 | 4 | 4
        0 | 0 | 0
    }

Power assertion example:

assert [[1,2,3,3,3,3,4]].first().unique() == [1,2,3]

// Output:
//
// Assertion failed:
// assert [[1,2,3,3,3,3,4]].first().unique() == [1,2,3]
//                          |       |        |
//                          |       |        false
//                          |       [1, 2, 3, 4]
//                          [1, 2, 3, 4]

REST endpoint testing example:

def "should return 201 with newly created task when new task has been created"() {
    given: "user is authenticated"
    def securityTokenId = createSecurityTokenWithUser(TEST_USER_ID)
    when: "client sends POST request to create a new task"
    def response = client.post(path: 'tasks', body: JSON_TASK, requestContentType: ContentType.JSON,
            headers: ['Authorization': generateAuthorizationHeader(securityTokenId)])
    then: "new task object is returned"
    response.status == 201
    response.data.id != null
    response.data.title == 'Reserve the table in Pillars for 20th of March'
    response.data.tags.size() == 1
    response.data.tags.first().name == 'planned'
}

 

I highly recommend anyone who is using Java to write tests in Spock instead. It is really easy to try and I believe, you will quickly realise how much better it is.

More about Spock to read and watch:

http://trishagee.github.io/presentation/groovy_vs_java/

[https://www.youtube.com/embed/VK2sMI5B1pY?feature=oembed]

comments powered by Disqus