51Testing软件测试论坛
标题: Spring Junit 读取WEB-INF下的配置文件 [打印本页]
作者: 雨中漫步_012 时间: 2019-1-28 14:15
标题: 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)
复制代码
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) |
Powered by Discuz! X3.2 |