register
other register

Tuesday, June 29, 2010

mockLogging in Grails Test

"By calling mockLogging(Class) in your unit test, any subsequently create instance of the given class will magically gain a working log property that echoes all log messages to the console. By default, debug and trace messages aren't included, to avoid excess output, but you can enable the debug messages by passing true as an optional second argument to mockLogging()." -- (Grails in Action)

class JetService {
  def doSomthing() {
    def a = "hello world"
    log.debug a
  }
}

class JetServiceUnitTests extends grails.test.GrailsUnitTestCase {
  void  testDoSomething() {
    mockLogging(JetService, true) // the second parameter "true" will output debug messages in the service method
    ...
  }
}

When running the unit test:

grails test-app unit:unit JetServiceUnit

In the report, you can see the debug message is shown in the System.output

No comments: