我正在构建一个使用MySQL数据库的Spring 3 MVC应用程序,并且最近将Flyway集成到解决方案中以管理数据库迁移.我已根据Flyway文档成功配置了我的applicationContext.xml,以便在应用程序启动时,Flyway将迁移到最新版本.
我无法让Flyway与我的单元/功能测试很好地配合使用.我使用Spring Data JPA作为我的数据访问层,并构建了一些JUnit测试来测试一些自定义查询.
我用于这些测试的应用程序配置是:
<jdbc:embedded-database id="dataSource" type="H2" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:medical_claims_tracker;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;MODE=MySQL;INIT=CREATE SCHEMA IF NOT EXISTS medical_claims_tracker" />
</bean>
<bean id="flyway" class="com.googlecode.flyway.core.Flyway" init-method="migrate">
<property name="dataSource" ref="dataSource"/>
<property name="schemas" value="medical_claims_tracker"/>
<property name="sqlMigrationPrefix" value="Migration_"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
当我运行单元测试时(通过Eclipse或使用Maven),我得到以下异常:
ERROR: org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@4cd4544] to prepare test instance [name.hines.steven.medical_claims_tracker.repositories.ExpenseRepositoryTest@1664a9b]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:103)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:73)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284) …Run Code Online (Sandbox Code Playgroud)