Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails Logging during integration tests

I've got few issues with Grails logging during test [running grails test-app, Grails 1.3.5]:

1

I've got some debug/info logging in my application and it works fine when running the app [grails run-app]. However when I want to test my app, none of it is written to the System.out/System.err files nor to file appender. How can I enable it?

I've got log.debug() and log.info() lines in my domain classes. in controllers and in classes located in src/groovy.

When I wanted to enable logging during test I just copied settings from my dev environment, and changed root logger from info to debug:

    appenders {
        file name: 'file', file: 'mylog.log'
    }

    root {
        debug 'stdout', 'file'
    }

    debug 'grails.app'


    error 'org.codehaus.groovy.grails.web.servlet',  //  controllers
            'org.codehaus.groovy.grails.web.pages', //  GSP
            'org.codehaus.groovy.grails.web.sitemesh', //  layouts
            'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
            'org.codehaus.groovy.grails.web.mapping', // URL mapping
            'org.codehaus.groovy.grails.commons', // core / classloading
            'org.codehaus.groovy.grails.plugins', // plugins
            'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
            'org.springframework',
            'org.hibernate',
            'net.sf.ehcache.hibernate'

    warn 'org.mortbay.log'

As I've said earlier. Everything works fine if I run the app in dev environment. It's only the test that I cannot see my logs.

Something to mention here: I can see my log.info() lines specified in the Test classes.

2 I cannot specify log.debug in Test classes. I'm getting missing method exception when trying to do log.debug. log.info works just fine. How come? I thought it's the same injection as within controllers/domains.

3 All information logged in Test classes during test are sent to System.err instead of System.out. Is grails even using the log4j configuration from Config.groovy?

Thanks in advance, Krystian

like image 427
Krystian Avatar asked Oct 26 '10 12:10

Krystian


People also ask

How do integration tests work in Grails?

Grails uses the test environment for integration tests and loads the application prior to the first test run. All tests use the same application state. Integration test methods run inside their own database transaction by default, which is rolled back at the end of each test method.

What are the advantages of using Grails in interactive mode?

Using Grails' interactive mode confers some distinct advantages when executing tests. First, the tests will execute significantly faster on the second and subsequent runs. Second, a shortcut is available to open the HTML reports in your browser: You can also run your unit tests from within most IDEs.

What is the rollback annotation in Grails?

In Grails 3.0 tests rely on grails.gorm.transactions.Rollback annotation to bind the session in integration tests. Though each test method transaction is rolled back, the setup () method uses a separate transaction that is not rolled back.

How to use Hibernate in Grails unit tests?

We are going to use a unit test for the dynamic finder with the help of HibernateSpec. It allows Hibernate to be used in Grails unit tests. It uses a H2 in-memory database. Test a limited set of domain classes, override getDomainClasses method and specify exactly which ones you wish to test.


1 Answers

what I do is just define my own logger and inject it into the controller or service in my test case.

    BasicConfigurator.configure() 
    LogManager.rootLogger.level = Level.DEBUG
    log = LogManager.getLogger("MailService")

    MailService.class.metaClass.getLog << {-> log}

I do not believe the log property is injected into test classes

I have also done this before which turned out to be more of a hack but worked

    def logger = new Expando(
            debug: { println it },
            info: { println it },
            warn: { println it },
            error: { println it })
    MailService.metaClass.getLog = {-> logger }
    MailServiceService.metaClass.getLog = {-> logger }
    MailIngestJobTests.metaClass.getLog = {-> logger }

Final Code Solution Here

like image 72
Aaron Saunders Avatar answered Oct 18 '22 21:10

Aaron Saunders