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  就可以使用指定的配置文件了。

  • 相关阅读:
    Linux命令:ls
    Log4j:log4j.properties 配置解析
    PostgreSQL: WITH Queries (Common Table Expressions)
    Linux使用SecureCRT上传下载
    PostgreSQL 自动输入密码
    Linux命令:rmdir
    Linux命令:mkdir
    Linux命令:pwd
    Linux命令:cd
    正则表达式 
  • 原文地址:https://www.cnblogs.com/yg_zhang/p/4236504.html
Copyright © 2011-2022 走看看