Home » JVM Languages » Groovy » Grails 3.3 has Spock 1.1

About Ted Vinke

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.

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns