Introduction

The template project, that you can download from the download page, follows the project structure outlined in the setup section. You will notice it features a build.gradle file for letting you build, test, run and deploy you project. This build file is using the Gradle build system, which is using the Groovy language for its build description DSL. This Gradle build file leverages key plugins that we'll describe below, and offers integration with the Spock testing framework for testing your groovlets, and with Geb for your functional tests.

Note: The template project conveniently provides the Gradle wrapper to run your code without having to install the Gradle runtime. You do not have to provide a Google App Engine SDK in your environment. It will automatically be downloaded by the build script.

Gradle build file

The Gradle build file uses the following Gradle plugins:

You'll find more information on the gae plugin and gaelyk plugin on their respective project pages. The pages describe all available configuration options and tasks in detail.

In a nutshell

You will find the following Gradle tasks handy:

Remark: Note that the Gradle Gaelyk plugin takes care of precompiling your Groovlets and Templates before upload to production, for faster startup times. The gaelykPrecompileGroovlet and gaelykPrecompileTemplate are called automatically and transparently during the build.

Testing with Spock

You can test your groovlets with the Spock testing framework. As an example, lets imagine we have the following groovlet, in WEB-INF/groovy/dataStoreGroovlet.groovy, which inserts some data in the datastore:

import com.google.appengine.api.datastore.*

def e = new Entity("person")
e.firstname = 'Bart'
e.lastname = 'Simpson'
e.save()

You could test such a Groovlet with the following Spock test, in src/test/groovy/DatastoreServiceSpec.groovy:

import groovyx.gaelyk.spock.*
import com.google.appengine.api.datastore.*
import static com.google.appengine.api.datastore.FetchOptions.Builder.*

class DatastoreServiceSpec extends GaelykUnitSpec {

    def setup() {
        groovlet 'dataStoreGroovlet.groovy'
    }

    def "the datastore is used from within the groovlet"() {
        given: "the initialised groovlet is invoked and data is persisted"
        dataStoreGroovlet.get()

        when: "the datastore is queried for data"
        def query = new Query("person")
        query.addFilter("firstname", Query.FilterOperator.EQUAL, "Marco")
        def preparedQuery = datastore.prepare(query)
        def entities = preparedQuery.asList(withLimit(1))

        then: "the persisted data is found in the datastore"
        def person = entities[0]
        person.firstname == 'Bart'
        person.lastname == 'Simpson'
    }
}

Apart for testing groovlets you can also unit test routes. A test for the following routes definition which is located in src/main/webapp/WEB-INF/routes.groovy:

get "/about", redirect: "/blog/2008/10/20/welcome-to-my-blog"
post "/other", forward: "/blog/2008/10/20/welcome-to-my-other-blog"

Could be located at src/test/groovy/RoutesSpec.groovy and look like:

class RoutesSpec extends GaelykRoutingSpec {

    def setup() {
        routing 'routes.groovy'
    }

    def "a get method may be routed"() {
        expect:
        get('/about')
    }

    def "an incorrectly routed method will not match"() {
        expect:
        !get('/aboutz')
    }

    def "a forward of a mapping may be configured"() {
        expect:
        with post('/other'), {
            matches
            destination == "/blog/2008/10/20/welcome-to-my-other-blog"
            redirectionType == FORWARD
        }
    }
}

Then, you could run the tests by executing the gradle command: gradlew test.

For further information, please have a look at the Spock support for Gaelyk.

Functional tests with Geb

With Geb, you can add functional tests to your Gaelyk application. You'll need to instruct Geb the address of your local dev server, in src/functionalTest/groovy/GebConfig.groovy:

baseUrl = 'http://localhost:8080/'

Then you can create your first smoke test as follows in src/functionalTest/groovy/SmokeSpec.groovy:

import geb.spock.GebSpec

class SmokeSpec extends GebSpec {
    void "main page title should be 'Gaelyk'"() {
        when:
        go ''

        then:
        title == 'Gaelyk'
    }
}

Then you can run the functional tests with: gradlew gaeFunctionalTest.

For more information, please have a look at this quick tutorial, and learn more about Geb.