Gorm-tools provides a set of test helpers to make the testing process more convenient
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
@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:
- buildParams - build GrailsParameter map from a given map
- flushAndClear - flushes and clears the session cache, the shortcut for RepoUtil.flushAndClear()
- flush - flushes the session cache, the shortcut for RepoUtil.flush()
- clear - clears the session cache, the shortcut for RepoUtil.clear()
- 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:
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:
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"
autowire - autowires bean properties of a given controller and returns the controller instance
- mockRender - adds mock of the
rendermethod to a metaclass of a given controller
- 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