在 grails-framework 中,一些对象正在使用日志。这通常由 grails 注入(inject)。它适用于 grails test-app
的执行。 .但是相同的测试(集成测试)在执行 grails test-app -integration
时失败。 .
这里出了什么问题,我可以以某种方式强制注入(inject)日志对象吗?
最佳答案
你用的是什么版本的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
-------------------------------------------------------
....
您是否在使用记录器类(或在任何以前的集成类或单元测试中覆盖记录器上的任何元类内容,然后不重新初始化元类?
关于logging - 为什么自动注入(inject)日志对象并不总是在 grails 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/301683/