Groovy Documentation

## grails.test
[Groovy] Class ControllerUnitTestCase

    java.lang.Object
      GroovyTestCase
          grails.test.GrailsUnitTestCase
              grails.test.MvcUnitTestCase
                  grails.test.ControllerUnitTestCase

    class ControllerUnitTestCase
    extends MvcUnitTestCase

Support class for writing unit tests for controllers. Its main job is to mock the various properties and methods that Grails injects into controllers. By default it determines what controller to mock based on the name of the test, but this can be overridden by one of the constructors.

Authors:
  Graeme Rocher
  Peter Ledbrook

Field Summary
`protected java.lang.Object` | `controller`

Fields inherited from class MvcUnitTestCase
forwardArgs, mockFlash, mockParams, mockRequest, mockResponse, mockSession, redirectArgs, renderArgs, webRequest

Constructor Summary
`ControllerUnitTestCase()` Creates a new test case for the controller that is in the same package as the test case and has the same prefix before "Controller" in its name.
`ControllerUnitTestCase(java.lang.Class controllerClass)` Creates a new test case for the given controller class.

Method Summary
`java.lang.Class` | `getControllerClass()`
`protected java.lang.Object` | `mockCommandObject(java.lang.Class clazz)` Mocks a command object class, providing a "validate()" method and access to the "errors" property.
`protected java.lang.Object` | `newInstance()`
`protected void` | `setUp()`
`protected void` | `setXmlRequestContent(java.lang.String content)` Sets an XML string as the body of the mock HTTP request.
`protected void` | `setXmlRequestContent(java.lang.String encoding, java.lang.String content)` Sets an XML string as the body of the mock HTTP request.
`protected void` | `setXmlRequestContent(groovy.lang.Closure c)` Sets the body of the mock HTTP request to some XML.
`protected void` | `setXmlRequestContent(java.lang.String encoding, groovy.lang.Closure c)` Sets the body of the mock HTTP request to some XML.

Methods inherited from class MvcUnitTestCase
getTestClass, newInstance, reset, tearDown

Methods inherited from class GrailsUnitTestCase
addConverters, enableCascadingValidation, loadCodec, mockConfig, mockController, mockDomain, mockFor, mockForConstraintsTests, mockLogging, mockTagLib, registerMetaClass, setUp, tearDown

Field Detail

### controller

    protected java.lang.Object controller

Constructor Detail

### ControllerUnitTestCase

    ControllerUnitTestCase()

  Creates a new test case for the controller that is in the same package as the test case and has the same prefix before "Controller" in its name. For example, if the class name of the test were `org.example.TestControllerTests`, this constructor would mock `org.example.TestController`.

### ControllerUnitTestCase

    ControllerUnitTestCase(java.lang.Class controllerClass)

  Creates a new test case for the given controller class.

Method Detail

### getControllerClass

    java.lang.Class getControllerClass()

### mockCommandObject

    protected java.lang.Object mockCommandObject(java.lang.Class clazz)

  Mocks a command object class, providing a "validate()" method and access to the "errors" property.

### newInstance

    protected java.lang.Object newInstance()

### setUp

    protected void setUp()

### setXmlRequestContent

    protected void setXmlRequestContent(java.lang.String content)

  Sets an XML string as the body of the mock HTTP request. The string is converted to bytes based on a UTF-8 encoding.

### setXmlRequestContent

    protected void setXmlRequestContent(java.lang.String encoding, java.lang.String content)

  Sets an XML string as the body of the mock HTTP request. The given string is converted to bytes according to the given encoding.

### setXmlRequestContent

    protected void setXmlRequestContent(groovy.lang.Closure c)

  Sets the body of the mock HTTP request to some XML. The XML is provided as an XmlBuilder-compliant closure. The XML is encoded to bytes using UTF-8.

### setXmlRequestContent

    protected void setXmlRequestContent(java.lang.String encoding, groovy.lang.Closure c)

  Sets the body of the mock HTTP request to some XML. The XML is provided as an XmlBuilder-compliant closure. The XML is encoded to bytes using the specified encoding.

Groovy Documentation