여기에 생각해 보았지만 정확히 묻지는 않았지만 grails 사용자 그룹에서 같은 질문을했습니다. 나는 지식을 전파하기 위해 나의 대답을 게시하고있다.
grails 사용자 그룹에서 설명한 것처럼 종속성 삽입에 의존하지 않고 클래스에서 def log = org.apache.commons.logging.LogFactory.getLog (this)를 명시 적으로 말하면 getLog를 LogFactory.
아래는 grails.tests.MockUtils.mockLogging에서 가져 와서 로거를 반환하도록 수정되었습니다. Grails의 1.1.1 및 설치에 대한 근사치를 사용
class LoggingEnabledTestCase extends GrailsUnitTestCase {
protected void setUp() {
super.setUp()
registerMetaClass(org.apache.commons.logging.LogFactory)
org.apache.commons.logging.LogFactory.metaClass.'static'.getLog = {instance ->
// This is taken from grails.tests.MockUtils and slightly changed to return a logger.
// Get the name of the class + the last component of the package
// (if it the class is in a package).
def pos = instance.class.name.lastIndexOf('.')
if (pos != -1) pos = instance.class.name.lastIndexOf('.', pos - 1)
def shortName = instance.class.name.substring(pos + 1)
// Dynamically inject a mock logger that simply prints the
// log message (and optional exception) to stdout.
def mockLogger = [
fatal: {String msg, Throwable t = null ->
println "FATAL (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
error: {String msg, Throwable t = null ->
println "ERROR (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
warn: {String msg, Throwable t = null ->
println "WARN (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
info: {String msg, Throwable t = null ->
println "INFO (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
debug: {String msg, Throwable t = null ->
println "DEBUG (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
trace: {String msg, Throwable t = null -> },
isFatalEnabled: {-> true},
isErrorEnabled: {-> true},
isWarnEnabled: {-> true},
isInfoEnabled: {-> true},
isDebugEnabled: {-> true},
isTraceEnabled: {-> false}] as Log
return mockLogger
}
}
protected void tearDown() {
super.tearDown()
}
}
로깅 문으로 흩어져있는 컨트롤러를 어떻게 테스트합니까? 나는 지금이 문제가있다 ?? –
테스트의 setUp() 메소드에서 mockLogging (MyDamnCoolCOntroller, true)을 사용하십시오. –