9ci Confidential


Gorm-tools provides a set of test helpers to make the testing process more convenient

Integration testing


This is a base trait that contains common logic for integration tests.

Test class should extend Specification class explicitly and implement IntegrationSpecHelper:

    class SomeSpec extends Specification implements IntegrationSpecHelper

Due to the fact that Spock conventional approach with setup/cleanup methods doesn't work with traits, this helper uses @Before / @After JUnit annotations to implement setup and cleanup logic.

As well as setup logic, IntegrationSpecHelper provides a bunch of useful methods which can be used in tests:

  1. buildParams - build GrailsParameter map from a given map
  2. flushAndClear - flushes and clears the session cache, the shortcut for RepoUtil.flushAndClear()
  3. flush - flushes the session cache, the shortcut for RepoUtil.flush()
  4. clear - clears the session cache, the shortcut for RepoUtil.clear()
  5. trackMetaClassChanges - start tracking all metaclass changes made after this call, so it can all be undone later

IntegrationSpecHelper provides an ability to execute application-specific setup/cleanup logic simply by chaining it with some custom trait:

    class SomeSpec extends Specification implements IntegrationSpecHelper, CustomSpecHelper

CustomSpecHelper may implement the next methods:

  1. specificSetup
  2. specificCleanup
  3. specificSetupSpec
  4. specificCleanupSpec

The behavior of these methods is equivalent to Spock conventional methods


Specific method is executed after the method defined in IntegrationSpecHelper


This trait extends IntegrationSpecHelper and provides common logic which can be useful when testing controllers. So there is no need to implement IntegrationSpecHelper explicitly. For example:

    class SomeSpec extends Specification implements ControllerIntegrationSpecHelper

By default it sets up mock request/response pair and injects application context bean.

ControllerIntegrationSpecHelper contains the next util methods:

  1. getControllerName - can be overridden in the test class to return the controller name. This name is appended to the request attributes. Returns null by default value.

    Also, can be specified as a property:

       String controllerName = "Org"

  2. autowire - autowires bean properties of a given controller and returns the controller instance

  3. mockRender - adds mock of the render method to a metaclass of a given controller
  4. getCurrentRequestAttributes - returns the current request attributes

Util methods from IntegrationSpecHelper are available as well.


In case of using this trait with a custom helper trait, the setup methods will be called in the next order: IntegrationSpecHelper -> ControllerIntegrationSpecHelper -> CustomSpecHelper

Unit testing