【问题标题】:Why does automatic injection of log-object not always work in grails?为什么自动注入日志对象并不总是在 grails 中工作?
【发布时间】:2010-09-23 00:49:51
【问题描述】:

在 grails-framework 中,一些对象正在使用日志。这通常由 grails 注入。它适用于grails test-app 的执行。但是相同的测试(集成测试)在执行 grails test-app -integration 时失败。

这里出了什么问题,我可以以某种方式强制注入日志对象吗?

【问题讨论】:

  • 您注入的服务会话或请求是否在范围内?我可以看到一个测试用例在这种情况下没有得到它(尽管你应该看到一个很好的异常消息)。

标签: logging grails groovy log4j code-injection


【解决方案1】:

我使用的是 1.3.2,登录测试工作正常。 只需确保您已为测试环境指定了 log4j 配置即可。

【讨论】:

    【解决方案2】:

    我刚刚遇到了这个确切的问题,非常可以通过调用 mockLogging 功能轻松解决:

    void testTheMagic() {
        mockLogging(MyMagicService)
        def testService = new MyMagicService()
        ...
    }
    

    (如果重要,使用 Grails 1.1.1)

    【讨论】:

    • 请注意:如果你有一个 setUp 方法,你需要调用 super.setUp() 如果你还没有
    【解决方案3】:

    您使用的是什么版本的 grails?在 1.0.4(最新版本)上,这两种情况都适用于我。

    我创建了一个新的空白应用并创建了一个带有集成测试的服务类:

    FooService.groovy:

    class FooService {
        def logSomething(message) {
            log.error(message)
            return true
        }
    }
    

    FooServiceTests.groovy:

    class FooServiceTests extends GroovyTestCase {
        def fooService
        void testSomething() {
        assert fooService.logSomething("it works")
        }
    }
    

    仅运行 test-app 时,我收到日志消息:

    % grails test-app             
    
    Welcome to Grails 1.0.4 - http://grails.org/
    ....
    -------------------------------------------------------
    Running 1 Integration Test...
    Running test FooServiceTests...
                            testSomething...[4174] service.FooService it works
    SUCCESS
    Integration Tests Completed in 440ms
    -------------------------------------------------------
    ...
    

    仅运行集成测试时,它也可以工作:

    % grails test-app -integration
    
    Welcome to Grails 1.0.4 - http://grails.org/
    ....
    -------------------------------------------------------
    Running 1 Integration Test...
    Running test FooServiceTests...
                        testSomething...[4444] service.FooService it works
    SUCCESS
    Integration Tests Completed in 481ms
    -------------------------------------------------------
    ....
    

    您是否在使用 logger 类(或在之前的任何集成类或单元测试中覆盖 logger 上的任何元类内容,而不是重新初始化元类?

    【讨论】:

    • 我们使用 Grails 1.0.3,但下次会切换到 1.0.4。我会看到,如果更新改变了事情。
    猜你喜欢
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多