Grails 3.3 has Spock 1.1

You’ll be glad to know that milestone 1 of Grails 3.3 has been released 3 days ago — and is also already available through SDKMAN!.

Not only has it upgraded its dependencies to Hibernate 5.1.5, Spring Framework 4.3.7, Spring Boot 1.5.3 and Gradle 3.5, but it also finally ships with the latest Spock 1.1 testing framework.

Spock is an old friend

We came to know Spock as one of our favorite testing and specification frameworks around. Around the time of Grails 1 we could use Spock 0.7 by including the Grails Spock plugin manually. Looking back through the Grails upgrade guides, as of 2.3 Spock became the default. Testing became definately more fun again. 🙂

Optional will return a default answer of Optional.empty()

Here’s one of the newer features you can now use when testing your Grails application.

You know that when there’s no interaction defined for a method call, mocks will return a default value based on their return type? If the return type is a boolean the mock will return false, for a number it will return 0 etcetera.

The — in Java 8 introduced — java.util.Optional had no support yet: when called on a Mock it would return null (leading to NPEs further down the road) and called on a Stub it would trigger a CannotCreateMockExcdeption.

Well, with Spock 1.1 not anymore. When there would be no interaction defined for a method returning an Optional, now by default an empty Optional is returned.

Directly from GitHub

import spock.lang.Specification

class OptionalSpec extends Specification {

  def "default answer for Optional should be Optional.empty()"() {
    given:
    TestService service = Stub()

    when:
    Optional<String> result = service.value

    then:
    !result.present
  }
}

interface TestService {
  Optional<String> getValue()
}

For a comprehensive overview of everything in Spock release 1.1 see the release notes.

One thought on “Grails 3.3 has Spock 1.1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s