zoukankan      html  css  js  c++  java
  • 在单元测试中指定log4j的配置文件

    在开发过程中,我们会使用到log4j来输出日志,我们希望在单元测试的时候,只看到部分日志信息,或者定义日志输出的级别。

     

    这个时候手工指定log4j的配置文件:

     

    具体做法如下:

    定义类如下:

    import java.io.FileNotFoundException;

    import org.junit.runners.model.InitializationError;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.util.Log4jConfigurer;

    public class JUnit4ClassRunner extends SpringJUnit4ClassRunner {
        static {
          try {
            Log4jConfigurer.initLogging("classpath:conf/log4j.xml");
          } catch (FileNotFoundException ex) {
            System.err.println("Cannot Initialize log4j");
          }
        }
        public JUnit4ClassRunner(Class<?> clazz) throws InitializationError {
          super(clazz);
        }
      }

    单元测试类使用如下:

     

    @RunWith(JUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:conf/app-test.xml"})
    @TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
    @Transactional
    public class BaseTestCase {

    }

    其他的单元测试类继承BaseTestCase  就可以使用指定的配置文件了。

  • 相关阅读:
    SqlHelper
    asp.net中窗口相关操作总结(javascript)
    ASP.NET顯示對話框
    为ASP.NET控件添加常用的JavaScript操作
    右键弹出菜单
    log4net的初使用
    QQ/MSN右下角弹出提示窗口
    简便无刷新文件上传系统
    简单的自动更新程序实
    SQL中的单记录函数
  • 原文地址:https://www.cnblogs.com/yg_zhang/p/4236504.html
Copyright © 2011-2022 走看看