Spring Junit 读取WEB-INF下的配置文件

测试环境:Spring3.0.5 + Junit4.8.1

 

配置文件放在class目录下:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");

WEB-INF下:

 

ApplicationContext applicationContext = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/spring.xml");

 

多个文件可用*表示

ApplicationContext applicationContext = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/spring-*.xml");

 

注解方式:

 

class:

@ContextConfiguration(locations={"classpath:spring.xml"})

WEB-INF:

@ContextConfiguration(locations={"file:WebRoot/WEB-INF/spring.xml"})

 

使用注解时,Junit报错:

java.lang.NoSuchFieldError: NULL at org.junit.runners.ParentRunner.<init>(ParentRunner.java:48) at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:59) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:104) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.junit.internal.requests.ClassRequest.buildRunner(ClassRequest.java:33) at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:28) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:28)

经检查是jar包冲突,我在建项目的时候,myeclipse有自动加入junit4,而后面我又自己加入Junit4.8.1。


如果报以下错误,则是junit包版本太低,需要4.5或以上的版本

Caused by: java.lang.ClassNotFoundException: org.junit.runners.BlockJUnit4ClassRunner  
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)  
    at java.security.AccessController.doPrivileged(Native Method)  
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)  
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)  
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)  
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)  
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)

你可能感兴趣的:(spring,MyEclipse,JUnit,jar,File,Class)