Groovy Documentation

grails.test
[Groovy] Class GrailsUnitTestCase

java.lang.Object
  GroovyTestCase
      grails.test.GrailsUnitTestCase

class GrailsUnitTestCase
extends GroovyTestCase

Support class for writing unit tests in Grails. It mainly provides access to various mocking options, while making sure that the meta- class magic does not leak outside of a single test.


Property Summary
java.lang.Object applicationContext

DefaultArtefactInfo domainClassesInfo

Keeps track of the domain classes mocked within a single test so that the relationships can be configured for cascading validation.

java.util.Map errorsMap

java.util.Set loadedCodecs

java.util.Map savedMetaClasses

 
Method Summary
protected void addConverters(java.lang.Class clazz)

protected void enableCascadingValidation()

Enables the cascading validation support for domain classes.

protected void loadCodec(java.lang.Class codecClass)

Loads the given codec, adding the "encodeAs...()" and "decode...()" methods to objects.

protected void mockConfig(java.lang.String config)

protected void mockController(java.lang.Class controllerClass)

Mocks a controller class, providing implementations of standard methods like render and redirect

protected void mockDomain(java.lang.Class domainClass, java.util.List instances = [])

Mocks a domain class, providing working implementations of the standard dynamic methods.

protected GrailsMock mockFor(java.lang.Class clazz, boolean loose = false)

Creates a new Grails mock for the given class.

protected void mockForConstraintsTests(java.lang.Class clazz, java.util.List instances = [])

Mocks the given class (either a domain class or a command object) so that a "validate()" method is added.

protected void mockLogging(java.lang.Class clazz, boolean enableDebug = false)

Provides a mock implementation of the "log" property for the given class.

protected void mockTagLib(java.lang.Class tagLibClass)

Mocks a tag library, providing the common web properties as well as "out", "throwTagError()", and an implementation of the "render" tag.

protected void registerMetaClass(java.lang.Class clazz)

Use this method when you plan to perform some meta-programming on a class.

protected void setUp()

protected void tearDown()

 

Property Detail

applicationContext

java.lang.Object applicationContext


domainClassesInfo

DefaultArtefactInfo domainClassesInfo
Keeps track of the domain classes mocked within a single test so that the relationships can be configured for cascading validation.


errorsMap

java.util.Map errorsMap


loadedCodecs

java.util.Set loadedCodecs


savedMetaClasses

java.util.Map savedMetaClasses


 
Method Detail

addConverters

protected void addConverters(java.lang.Class clazz)


enableCascadingValidation

protected void enableCascadingValidation()
Enables the cascading validation support for domain classes. This should be called after all the relevant domain classes have been mocked.


loadCodec

protected void loadCodec(java.lang.Class codecClass)
Loads the given codec, adding the "encodeAs...()" and "decode...()" methods to objects.
Parameters:
codecClass - The codec to load, e.g. HTMLCodec.


mockConfig

protected void mockConfig(java.lang.String config)


mockController

protected void mockController(java.lang.Class controllerClass)
Mocks a controller class, providing implementations of standard methods like render and redirect


mockDomain

protected void mockDomain(java.lang.Class domainClass, java.util.List instances = [])
Mocks a domain class, providing working implementations of the standard dynamic methods. A list of domain instances can be provided as a source of data for the methods, in particular the queries.
Parameters:
domainClass - The class to mock.
instances - An optional list of domain instances to use as the data backing the dynamic methods.


mockFor

protected GrailsMock mockFor(java.lang.Class clazz, boolean loose = false)
Creates a new Grails mock for the given class. Use it as you would use MockFor and StubFor.
Parameters:
clazz - The class to mock.
loose - If true, the method returns a loose- expectation mock, otherwise it returns a strict one. The default is a strict mock.


mockForConstraintsTests

protected void mockForConstraintsTests(java.lang.Class clazz, java.util.List instances = [])
Mocks the given class (either a domain class or a command object) so that a "validate()" method is added. This can then be used to test the constraints on the class.


mockLogging

protected void mockLogging(java.lang.Class clazz, boolean enableDebug = false)
Provides a mock implementation of the "log" property for the given class. By default, debug and trace levels are ignored but you can enable printing of debug statements via the enableDebug argument.
Parameters:
clazz - The class to add the log method to.
enableDebug - An optional flag to switch on printing of debug statements.


mockTagLib

protected void mockTagLib(java.lang.Class tagLibClass)
Mocks a tag library, providing the common web properties as well as "out", "throwTagError()", and an implementation of the "render" tag.


registerMetaClass

protected void registerMetaClass(java.lang.Class clazz)
Use this method when you plan to perform some meta-programming on a class. It ensures that any modifications you make will be cleared at the end of the test.
Parameters:
clazz - The class to register.


setUp

protected void setUp()


tearDown

protected void tearDown()


 

Groovy Documentation