To specify a configuration file for log4j in a unit test

Source: Internet
Author: User

During the development process, we use the log4j to output the logs, and we want to see only a subset of the log information at the time of unit testing, or to define the level of log output.

This time manually specify the log4j configuration file:

The following are the specific practices:

The definition classes are as follows:

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);
}
}

The Unit test class is used as follows:

@RunWith (junit4classrunner. Class)
@ContextConfiguration (locations={"Classpath:conf/app-test.xml"})
@TransactionConfiguration (transactionmanager= "TransactionManager", Defaultrollback=false)
@Transactional
public class Basetestcase {

}

Other unit test classes inherit basetestcase to use the specified configuration file.

To specify a configuration file for log4j in a unit test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.