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