JUnit needs to use log4j at run time, but the log4j configuration file needs to be loaded at boot time, This requires the first to be able to find the Log4j.properties file, in JUnit, the default is to find the Log4j.properties file from the src file directory, but if the log4j.properties file is not in the SRC directory will report the following error:
Cannot Initialize log4j
Log4j:warn No appenders could is found for logger ' ( Org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
Log4j:warn Please initialize the log4j system properly.
Log4j:warn http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
General log4j.properties files are placed in a specific directory, and in order to find log4j.properties files, you can use the following methods:
Create a class of Junit4classrunner that inherits Springjunit4classrunner
public class Junit4classrunner extends Springjunit4classrunner {
static {
try {
Log4jconfigurer.initlogging ("Classpath:propertis/log4j.properties"); Specifies log4j.properties file location
} catch (FileNotFoundException ex) {
System.err.println ("Cannot Initialize log4j");
}
Public
junit4classrunner (class<?> clazz) throws Initializationerror {
super (clazz);
}
}
The test class is then configured as follows:
@RunWith (Junit4classrunner.class)
@ContextConfiguration (locations = {"Classpath:config/spring-context.xml"} Public
class Testjava {
private Logger Logger = Logger.getlogger (testjava.class);
@Autowired
private testservicei testservice;
@Test public
void Testfindallusers () {
logger.info (testservice.findallusers (). Size ());
}
Note: If the spring version and JUnit are incompatible, the following error will be reported:
Java.lang.noclassdeffounderror:org/junit/runners/model/multiplefailureexception at Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.withAfterClasses (Springjunit4classrunner.java : 188) at Org.junit.runners.ParentRunner.classBlock (parentrunner.java:145) at Org.junit.runners.ParentRunner.run (Pa rentrunner.java:235) at Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run ( springjunit4classrunner.java:163) at Org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run ( junit4testreference.java:86) at Org.eclipse.jdt.internal.junit.runner.TestExecution.run (testexecution.java:38) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:459) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:675) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (remotetestrunner.java:382) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (Remotetestrunner.java:192) caused by:java.lang.ClassNotFoundException:org.junit.runners.model.MultipleFailureException a T Java.net.URLClassLoader.findClass (urlclassloader.java:381) at Java.lang.ClassLoader.loadClass (Classloader.java : 424) at Sun.misc.launcher$appclassloader.loadclass (launcher.java:331) at Java.lang.ClassLoader.loadClass (Classloa der.java:357) ... Ten more
If this happens, you can try a different version of JUnit