Logging in Grails 2.x unit tests

465 views
Skip to first unread message

Natalya Portnov

unread,
May 19, 2013, 3:03:53 PM5/19/13
to groo...@googlegroups.com
   Can anyone please explain why log oblect is not auto-injected in unit tests? In the example below isn't @TestFor(ProcedureController) suppose to initialize log4j 
from my Config.groovy?

...ProcedureControllerTests.groovy
package my-package

import org.junit.*
import grails.test.mixin.*

@TestFor(ProcedureController)
@Mock([Procedure, ProcedureType, ProcedureUser, Location])
class ProcedureControllerTests {

    def populateValidParams(params) {
    ...
        
        def p = new Procedure(params)
        assertFalse p.validate()
        p.errors.each{ log.debug "err: $it" }
    }  
...
}    

Config.groovy
...
log4j = {
     debug 'my-package'
}


Running unit tests:
Using grails version 2.2.1 in this shell.
natalya:my-app ngp$ grails test-app unit: -echoOut
| Running 12 unit tests... 5 of 12
--Output from testSave--


| Failure:  testDelete(edu.umn.ncs.ProcedureControllerTests)
|  Assertion failed:

assert procedure.save() != null
       |         |      |
       |         null   false
       Hard drive: 2  Procedure type: backup   performed by: backupuser



| Failure:  testDelete(edu.umn.ncs.ProcedureControllerTests)
|  groovy.lang.MissingMethodException: No signature of method: java.util.logging.Logger.debug() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [err: org.grails.datastore.mapping.validation.ValidationErrors: 1 errors


Thanks,
Natalya

--
Natalya G. Portnov
IT Professional
University of Minnesota

Health Studies
200 Oak St SE, Suite 350
Minneapolis, MN 55455

T: 612.625.4329
F: 612.624.3370
E: n...@umn.edu

glenn opdycke-hansen

unread,
May 19, 2013, 5:26:13 PM5/19/13
to groo...@googlegroups.com
I found the following section in the 

Mastering Grails: Mock testing with Grails
, by Scott Davis


This time, however, the failure isn't due to the lack of metaprogramming on a domain class. It's due to the lack of dependency injection. Specifically, all Grails artifacts get a log object injected into them at run time so that they can easily log messages for later review.

To inject a mock logger for testing purposes, wrap the AdminService class in a mockLogging() method call, as shown in Listing 17:


Listing 17. Service test that will pass, thanks to mockLogging()

void testRestartServer() {
  def jdoe = new User(name:"John Doe", role:"user")
  def suziq = new User(name:"Suzi Q", role:"admin")

  mockLogging(AdminService)
  def adminService = new AdminService()
  assertTrue adminService.restartServer(suziq)
  assertFalse adminService.restartServer(jdoe)
}

This time, the test passes as expected. And any log output is sent to System.out. Remember that you can see this output in the HTML reports.

Hope this helps.

-glenn



--glenn


--
--
You received this message because you are subscribed to the "Groovy Users of Minnesota" group.
 
To post to this group, send email to groo...@googlegroups.com
To unsubscribe from this group, send email to groovymn-u...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/groovymn?hl=en
---
You received this message because you are subscribed to the Google Groups "Groovy Users of Minnesota" group.
To unsubscribe from this group and stop receiving emails from it, send an email to groovymn+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

John Engelman

unread,
May 19, 2013, 6:21:49 PM5/19/13
to groo...@googlegroups.com
In Grails 2.x the logging framework was changed from using metaclass injection to an AST that's applied. Thus the loggers are applied at compile time. Therefore you should not be using the old mockLogging() method...in fact, in not sure it is available via the Grails 2 test mixins.

It looks like you are trying to call the logger from the test class itself. I'm not sure if a logger is compiled into each test class. Typically, I would just write to System.out or System.err if I wanted to print out stuff from a test case.

Reply all
Reply to author
Forward
0 new messages