2014-05-15 149 views
1

我有一個使用Spring MVC 3.2.1的現有Java應用程序。現有的單元測試一切正常。現在我正在嘗試進行新的單元測試,第一次使用@WebAppConfiguration註釋,並且它出現了一個古怪的錯誤。WebAppConfiguration無法與ContextConfiguration一起使用?

這是我的新的單元測試,從this page幾乎逐字:

@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@ContextConfiguration(locations={"classpath:war-test-context.xml"}) 
@Transactional 
public class GenericPageControllerTest { 

    @Autowired 
    private WebApplicationContext webApplicationContext; 

    private MockMvc mockMvc; 

    @Before 
    public void setup() { 
     this.mockMvc = webAppContextSetup(this.webApplicationContext).build(); 
    } 

    @Test 
    public void testControllerSecurityTestThingyTestWithALongNameTest() throws Exception { 
     this.mockMvc.perform(get("/payOptions.html")) 
     .andExpect(redirectedUrl("/login.html")); 
    } 

} 

這裏是我得到的錯誤:

java.lang.IllegalStateException: Failed to load ApplicationContext 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' defined in class path resource [war-test-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [war-test-context.xml]: Cannot resolve reference to bean 'persistenceUnitManager' while setting bean property 'persistenceUnitManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'persistenceUnitManager' defined in class path resource [war-test-context.xml]: Cannot resolve reference to bean 'caDataSource' while setting bean property 'dataSources' with key [TypedStringValue: value [caDataSource], target type [null]]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'caDataSource': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Failed to populate database; nested exception is org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from ServletContext resource [/database/myapp_schema.sql] 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [war-test-context.xml]: Cannot resolve reference to bean 'persistenceUnitManager' while setting bean property 'persistenceUnitManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'persistenceUnitManager' defined in class path resource [war-test-context.xml]: Cannot resolve reference to bean 'caDataSource' while setting bean property 'dataSources' with key [TypedStringValue: value [caDataSource], target type [null]]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'caDataSource': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Failed to populate database; nested exception is org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from ServletContext resource [/database/myapp_schema.sql] 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'persistenceUnitManager' defined in class path resource [war-test-context.xml]: Cannot resolve reference to bean 'caDataSource' while setting bean property 'dataSources' with key [TypedStringValue: value [caDataSource], target type [null]]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'caDataSource': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Failed to populate database; nested exception is org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from ServletContext resource [/database/myapp_schema.sql] 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'caDataSource': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Failed to populate database; nested exception is org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from ServletContext resource [/database/myapp_schema.sql] 
Caused by: org.springframework.dao.DataAccessResourceFailureException: Failed to populate database; nested exception is org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from ServletContext resource [/database/myapp_schema.sql] 
Caused by: org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from ServletContext resource [/database/myapp_schema.sql] 
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/database/myapp_schema.sql] 

僅使用@ContextConfiguration註釋找到這個文件並創建實例其他測試這個bean很好。誰能告訴我爲什麼@WebAppConfiguration bean正在讓它嘔吐?這裏的配置部分它告訴我將無法正常工作:

<jdbc:embedded-database id="caDataSource" type="HSQL"> 
    <jdbc:script location="database/myapp_schema.sql"/> 
    <jdbc:script location="database/myapp_data.sql"/> 
</jdbc:embedded-database> 

的文件是在類路徑中,但他們在不同的項目 - 這可能是唯一的皺紋。任何人有任何想法,或方便的鏈接?

回答

0

看起來像this is the reason

當您使用WebApplicationContext時,您將失去使用classpath:configuration或其他東西的能力,並且僅限於/ src/main/webapp(或提供的其他位置)中的任何內容。

WebAppContext javadoc是here

相關問題