Ratpack Cookbook

Ratpack: mocking Session object in GroovyRequestFixture test

Ratpack allows you unit test handlers using GroovyRequestFixture class. The good thing about this approach is that it does not require running the whole application and you can quickly test if the handler does what you expect. However, if you retrieve objects from Raptack’s registry you will run into a problem - registry in this case is empty.

In some cases you may find mocking Session object useful. Especially if you only want to retrieve specific object or value from session and do something with it. GroovyRequestFixture.handle(chain, closure) gives you an access to registry through the closure passed in the second parameter.

GroovyRequestFixture.handle(yourHandler) {
    registry { r ->
        r.add(Session, mockSession)

Here we have registered mockSession to be injected anytime Session instance is being retrieved from the registry. Keep in mind that mock object does nothing by default (e.g. it return null values for methods invocation) so you will have to "configure" your mock object to return something significant. For instance:

Session mockSession = Mock(Session) {
    get('test') >> Promise.value(Optional.of('Lorem ipsum'))

will return Lorem ipsum value (as a promise of optional) for session.get('test').

And here you can find a full example:

import groovy.transform.CompileStatic
import ratpack.exec.Promise
import ratpack.groovy.handling.GroovyChainAction
import ratpack.groovy.test.handling.GroovyRequestFixture
import ratpack.http.Status
import ratpack.jackson.internal.DefaultJsonRender
import ratpack.session.Session
import spock.lang.Specification

import static ratpack.jackson.Jackson.json

class MockSessionSpec extends Specification {

    final Session session = Mock(Session) {
        get('test') >> Promise.value(Optional.of('Lorem ipsum'))

    final GroovyChainAction chainAction = new GroovyChainAction() {
        void execute() throws Exception {
            get('foo') {
                get(Session).get('test').map { Optional<String> o ->
                }.flatMap { value ->
                }.then {
                    render(json([message: it]))

    def "should retrieve message from Session object"() {
        def result = GroovyRequestFixture.handle(chainAction) {
            uri 'foo'
            method 'GET'
            registry { r ->
                r.add(Session, session)

        result.status == Status.OK

        result.rendered(DefaultJsonRender).object == [message: 'Lorem ipsum']

Did you like this article?

Consider buying me a coffee