Testing

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

Integration testing

IntegrationSpecHelper

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 -
  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

Note

Specific method is executed after the method defined in IntegrationSpecHelper

ControllerIntegrationSpecHelper

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.

Note

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

DataRepoTest

TODO

DomainRepoCrudSpec

TODO

DomainRepoTest

TODO

GormToolsSpecHelper

TODO

GormToolsTest

TODO

JsonViewSpecSetup

TODO

MockJdbcIdGenerator

TODO

ExternalConfigAwareSpec

The trait makes it possible to load external config during unit tests. If external-config plugin is installed, the configuration defined in config.locations will be loaded and be made available to unit tests. DataRepoTest extends ExternalConfigAwareSpec so subclasses does not need to extend it explicitely.

Note: ExternalConfigAwareSpec uses doWithSpring to define ExternalConfigLoader bean, so that it runs during Grails application lifecycle. If the test overrides doWithSpring, the method in trait will not run. So the test class should explicitely call ExternalConfigAwareSpec .super.doWithSpring