user3120173
user3120173

Reputation: 1788

WebAppConfiguration doesn't play well with ContextConfiguration?

I have an existing Java app using Spring MVC 3.2.1. The existing unit tests all work fine. Now I'm trying to a new unit test, using the @WebAppConfiguration annotation for the first time, and it's blowing up with a wacky error.

Here's my new unit test, taken almost verbatim from 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"));
    }

}

Here's the error I'm getting:

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]

Other tests using just the @ContextConfiguration annotation find this file and instantiate this bean just fine. Can anyone tell me why the @WebAppConfiguration bean is making it throw up? Here's the configuration section it's telling me won't work:

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

The files are on the classpath, but they're in a different project - that might be the only wrinkle. Anyone got any ideas, or handy links?

Upvotes: 2

Views: 1649

Answers (1)

user3120173
user3120173

Reputation: 1788

Looks like this is the reason.

When you use WebApplicationContext you lose the ability to use classpath: configuration or other stuff, and are limited to whatever's in /src/main/webapp (or another location as provided).

WebAppContext javadoc is here.

Upvotes: 1

Related Questions