开发者

Logging in a GrailsUnitTestCase?

开发者 https://www.devze.com 2023-01-02 18:52 出处:网络
How do I setup logging in a grails unit-test?开发者_Python百科 When I try log.info or log.debug, the .txt output files are empty, even after I tried adding a console appender. What\'s going on here?T

How do I setup logging in a grails unit-test?

开发者_Python百科

When I try log.info or log.debug, the .txt output files are empty, even after I tried adding a console appender. What's going on here?


This might help, it's taken from the 1.2 release notes

By default, grails does not show the output from your tests. You can make it do so by passing -echoOut and/or -echoErr to test-app:

grails test-app -echoOut -echoErr


If you extend GrailsUnitTestCase, you ought to be able to use mockLogging(), but the appenders you set up in Grails config won't be applied in a unit test, which works in isolation from the real framework. They'd only be available in integration tests.


I wasn't able to use mockLogging in grails 1.3.7 with GrailsUnitTestCase either. I think there is probably a bug and it may work in Grails 2.0. Here is what I did to work around it:

class Foo {
    String name

    Long invokeLogTest(String key) {
        if (key.empty) {
            log.error("key was sent as empty string")
            return 10
        }  
    }
}

void testErrorCase() {
    def f = new Foo(name:'jp')
    f.metaClass.log = [error:{}]
    assert 10 == f.invokeLogTest("")
}
0

精彩评论

暂无评论...
验证码 换一张
取 消