Grails 3.3 Integration Testing with Spock Mocks

It is easy to use the Spock Framework, shipped with Grails, to mock or stub a collaborator (such as a service) in our Grails unit tests.

The Testing chapter explains a bit about mocking collaborators, doWithSpring/doWithConfig callback methods, the FreshRuntime annotation to mock beans in tests — but they’re mainly for unit testing.

How about mocking beans in an integration test?

Continue reading “Grails 3.3 Integration Testing with Spock Mocks”

Avoid NullPointerException: Safe Navigation with Groovy

We know it’s all too common in Java to get a NullPointerException when we use an object reference which is null. This happens when our code tries to access a method or field of an object, or element of an array when there’s no instance present – e.g. it refers to null.

class Animal {
 String name
 Animal parent
}

def animal = new Animal(name: "Bella") // no parent

Continue reading “Avoid NullPointerException: Safe Navigation with Groovy”