我正在构建一个使用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) 我正在尝试创建一个使用嵌入式H2数据库的测试。但是我必须更改spring.datasource.url,我不能使用spring boot创建的默认值。(这是因为我必须将H2数据库的模式更改为MYSQL)
这是我的test class:
@JdbcTest
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
public class DemoApplicationTests {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
DataSource dataSource;
@Test
public void contextLoads() {
System.out.println(dataSource);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的application-test.properties:
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL
spring.datasource.username=dbuser
spring.datasource.password=dbpass
Run Code Online (Sandbox Code Playgroud)
我的依赖:
compile('org.springframework.boot:spring-boot-starter-batch')
runtime('com.h2database:h2')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '1.5.3.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.3.RELEASE'
Run Code Online (Sandbox Code Playgroud)
控制台输出:
启动嵌入式数据库:url ='jdbc:h2:mem:bfad6b71-3e2d-4a47-a32d-c76988b3c5f6; DB_CLOSE_DELAY = -1; DB_CLOSE_ON_EXIT = false',用户名='sa'
我希望网址是这样的:jdbc:h2:mem:testdb,我也希望它使用MODE=MYSQL设置。
我试图关注这篇文章,但是没有用。