简体   繁体   English

在Grails JUnit测试案例中记录信息的正确方法是什么?

[英]What is the correct way to log information in a Grails JUnit test case?

I have recently started using Grails for a project. 我最近开始将Grails用于一个项目。 I am working from the Intellij Idea environment. 我正在Intellij Idea环境中工作。

I am writing unit test in order to test the services I create. 我正在编写单元测试以测试我创建的服务。

I am unable to log information from the test cases. 我无法记录测试案例中的信息。 No matter the log level I use there is no message displayed in the console. 无论我使用的日志级别如何,控制台中都不会显示任何消息。

Here is my log4j configuration in Config.groovy: 这是我在Config.groovy中的log4j配置:

log4j = {
    appenders {
        console name: 'stdout', layout: pattern(conversionPattern: '%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L %x - %m%n')
    }

    root {
        info    'a1'
        additivity = true
    }

    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.springframework',
           'org.hibernate',
           'org.apache',
           'net.sf.ehcache.hibernate'

    debug  'grails.app.controllers'
           'hibernate.SQL'
            'grails.app'
}

I also have to mention the fact that when I run the test case I get the following messasge at the beginning but from what I have read it is just a warning and it should not influence the test case. 我还必须提到一个事实,当我运行测试用例时,会在开始时得到以下消息,但从我阅读的内容来看,这只是一个警告,并不影响测试用例。

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/abujdei/grails/grails-2.3.7/dist/grails-plugin-log4j-2.3.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/abujdei/grails/grails-2.3.7/lib/org.slf4j/slf4j-simple/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http:/www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.GrailsSlf4jLoggerFactory]
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http:/logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

I have been struggling with this for a couple od hours now. 我已经为此努力了两个多小时。

In Intellij's Grails View, when you rightclick a testable node (package, class or method), under Run you should have two options: 在Intellij的Grails视图中,当您右键单击可测试的节点(包,类或方法)时,在“运行”下,您应具有两个选项:

  • using junit runner (standard test icon, not aware of your grails configuration) 使用junitRunner(标准测试图标,不知道您的grails配置)
  • using grails test-app command (grails icon, I think you want to use this one.) 使用grails test-app命令(grails图标,我想您想使用这一命令。)

Note that if your version of grails uses forked test execution by default, you have to either ( source ): 请注意,如果默认情况下您的grails版本使用分叉测试执行,则您必须( ):

  • disable it: grails.project.fork.test = false in grails-app/conf/BuildConfig.groovy 禁用它:grails-app / conf / BuildConfig.groovy中的grails.project.fork.test = false
  • copy <Intellij_install_dir>/plugins/Grails/grails-rt.jar to your project's lib. <Intellij_install_dir>/plugins/Grails/grails-rt.jar复制到项目的lib中。

You should reference the console appender in the root-block: 您应该在root块中引用控制台附加程序:

// ...
root {
  info 'stdout'
  additivity = true
}
// ...

I've put an example for logging in different environments at snipplr , not using the console appender, but it might still be useful. 我在snipplr上提供了一个用于在不同环境中登录的示例,而不使用控制台附加程序,但是它可能仍然有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM