Groovy

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.

Reference: Grails 3.3 has Spock 1.1 from our JCG partner Ted Vinke at the Ted Vinke’s Blog blog.

Ted Vinke

Ted is a Java software engineer with a passion for Web development and JVM languages and works for First8, a Java Web development company in the Netherlands.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button