在单元测试中是否有任何简单的slf4j用法模式? [英] Is there any simple pattern of slf4j usage in unit tests?

查看:130
本文介绍了在单元测试中是否有任何简单的slf4j用法模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用JUnit4和Hibernate3。 Hibernate依赖于Slf4j,因此我的项目也包含了这个库。现在我想在单元测试中使用Slf4j来记录补充测试信息。您能否提供一个简短的示例,说明我的单元测试应该如何只记录一行文本?在多次测试中最好没有代码重复

I'm using JUnit4 and Hibernate3 in my project. Hibernate depends on Slf4j and thus my project includes this library as well. Now I'd like to use Slf4j in unit tests in order to log supplementary testing information. Could you please provide a short example of how my unit test should look like in order to log just one line of text? Preferably without code duplication in multiple tests.

推荐答案

我也想在我的JUnit测试中使用slf4j对于我的DAO课程。当您创建新测试或修改旧测试时,它会有所帮助。我通常将我的旧日志输出保留在调试级别,并在信息级别创建新的日志记录语句,同时我仍在积极处理该方法中的代码。我的一个JUnit类看起来像这样:

I also like to use slf4j in my JUnit tests for my DAO class. It does help when you are creating a new test or modifying an old one. I usually leave my old logging output at debug level, and make my new logging statements at info level while I'm still actively working on the code in that method. One of my JUnit classes would look something like this:

package com.example.mydao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// other imports not shown...

public class TestMyDAO extends TestCase {

    private static final Logger logger = 
        LoggerFactory.getLogger(TestMyDAO.class);


    public void testA() {
        logger.debug("Logging from testA() method");
    }

    public void testB() {
        logger.debug("Logging from testB() method");
    }

    public void testThatIAmWorkingOn() {
        logger.info("Logging from my new test method at INFO level");
    }

}

我正在使用log4j作为实际的日志记录提供程序,所以我的 log4j.xml 配置文件如下所示:

I'm using log4j as the actual logging provider, so my log4j.xml configuration file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
    <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p  [%c{1}] %m %n" />
        </layout>
    </appender>

    <logger name="com.example.mydao" additivity="false">
        <level value="INFO" />
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="org.hibernate" additivity="false">
        <level value="WARN" />
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="org.hibernate.connection.DriverManagerConnectionProvider" additivity="false">
        <level value="INFO" />
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="org.hibernate.connection.C3P0ConnectionProvider" additivity="false">
        <level value="INFO" />
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="com.mchange" additivity="false">
        <level value="WARN" />
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="com.mchange.v2.resourcepool.BasicResourcePool" additivity="false">
        <level value="INFO" />
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource" additivity="false">
        <level value="INFO" />
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="com.mchange.v2.c3p0.C3P0Registry" additivity="false">
        <level value="INFO" />
        <appender-ref ref="consoleAppender"/>
    </logger>

    <root>
        <priority value ="WARN" />
        <appender-ref ref="consoleAppender"/>
    </root>

</log4j:configuration>

这给了我来自JUnit类的信息级输出,以及来自Hibernate的一些有用的东西运行时和Hibernate使用的其他库。根据自己的喜好调整。

This gives me the info-level output from my JUnit class, as well as some useful stuff from Hibernate runtime and other libraries used with Hibernate. Adjust to your own taste.

最后,我需要确保在执行JUnit测试时,所有以下库都在我的类路径中:

Finally, I need to make sure that all of the following libraries are in my classpath when I execute the JUnit tests:


  • slf4j-api-1.6.0.jar

  • slf4j-log4j12-1.6.0.jar

  • log4j-1.2.16.jar

  • log4j.xml (我的配置文件,如上所示)

  • 一些JUnit运行时JAR的版本

  • 在生产环境中运行应用程序时通常存在的所有JAR

  • slf4j-api-1.6.0.jar
  • slf4j-log4j12-1.6.0.jar
  • log4j-1.2.16.jar
  • log4j.xml (my configuration file, shown above)
  • Some version of the JUnit runtime JAR
  • All the JARs normally present when running your application in production

这是我在使用log4j时所做的。如果使用其他日志记录实现,则相应地进行调整。如果你使用不同版本的slf4j并不重要,只要API和实现JAR是相同的版本(我的是1.6.0)。

This is what I do when using log4j. If you use a different logging implementation, then adjust accordingly. It doesn't matter if you're using a different version of slf4j, as long as the "API" and implementation JARs are the same version (mine are both 1.6.0).

这篇关于在单元测试中是否有任何简单的slf4j用法模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆