我有一个使用Spring MVC 3.2.1的现有Java应用程序。现有的单元测试一切正常。现在,我正在尝试进行新的单元测试,这是第一次使用@WebAppConfiguration批注,并且它突然出现了古怪的错误。

这是我的新单元测试,几乎是从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豆为何将其抛出?这是配置部分,它告诉我将无法使用:

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


这些文件在类路径上,但是它们在另一个项目中-可能是唯一的麻烦。任何人有任何想法或方便的链接吗?

最佳答案

看起来像this is the reason

使用WebApplicationContext时,您将失去使用classpath:配置或其他内容的能力,并且只能使用/ src / main / webapp(或提供的其他位置)中的任何内容。

WebAppContext javadoc是here

09-11 03:12