![](/img/trans.png)
[英]Why does file: prefix not working in @ContextConfiguration with my test using spring framework?
[英]Why does Spring's @ContextConfiguration run when given a nonexistent file?
我正在調試某人的Java,並嘗試確定spring config文件是否已正確加載。 事實似乎使Spring默默地忽略了不存在的文件,這使情況變得復雜。 例如,以下運行正常:
import org.springframework.test.context.*;
import org.junit.runner.*;
import org.junit.Test;
import org.springframework.test.context.junit4.*;
@ContextConfiguration(locations={"classpath:I_DONT_EXIST.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class Test3 {
public static void main(String [] args){
System.out.println("Hello World");
}
@Test
public void testSomething() {
}
}
盡管指示的文件I_DONT_EXIST.xml在任何地方都不存在。 有沒有一種方法可以修改它,如果指定的文件不存在,運行時將報錯?
Spring會抱怨缺少上下文xml文件。
我不確定您如何使用粘貼的類,但是為了啟動spring上下文進行測試(已在JUnit框架上執行),可以按如下方式注釋類:
@ContextConfiguration(locations={"classpath:I_DONT_EXIST.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class Test2 {
public static void main(String [] args){
System.out.println("Hello World");
}
@Test
public void testSomething() {
}
}
嘗試作為JUnit測試執行時,將引發以下異常:
Caused by: java.io.FileNotFoundException: class path resource [I_DONT_EXIST.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157) ~[org.springframework.core-3.2.4.RELEASE.jar:3.2.4.RELEASE]
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ~[org.springframework.beans-3.2.4.RELEASE.jar:3.2.4.RELEASE]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.