最近spring测试中出现了一个很奇怪的错误:找不到applicationContext.xml文件
Java.io.FileNotFoundException: class path resource [ applicationContext.xml] cannot be opened because it does not exist
但是经过反复检查:resource目录和classpath都已经配置,并没有发现任何问题。
为了找这个问题简直要抓狂了,什么把xml文件复制到test目录的class目录下,把xml文件复制到src目录都试过了,都解决不了问题!最后实在没有办法了,准备去打游戏放松一下。
然后突然灵光一闪:发现了问题所在:原来是classpath后面的冒号有空格!
这才是问题的关键,去掉空格就能找到xml文件!
注意:这里的classpath:后面不能有空格!否则就会出现找不到xml文件的问题!
@RunWith(SpringJUnit4ClassRunner.class)
// 注意:这里的classpath:后面不能有空格!否则就会出现找不到xml文件的问题!
@ContextConfiguration( locations = "classpath: applicationContext.xml")
//@ContextConfiguration( locations = "classpath:context.xml")
public class TestBeanInject extends TestCase {
@Autowired
private ActionService actionService;
这个问题隐藏的很深,很难发现!所以分享给各位,希望对大家有帮助!