use*_*003 7 java junit spring spring-mvc flyway
我使用Flyway来管理我的Spring MVC应用程序中的数据库状态.
我在我的servlet上下文XML文件中配置它完全按照他们的文档中的建议
<bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">
    <property name="dataSource" ref="..."/>
    ...
</bean>
<!-- The rest of the application (incl. Hibernate) -->
<!-- Must be run after Flyway to ensure the database is compatible with the code -->
<bean id="sessionFactory" class="..." depends-on="flyway">
    ...
</bean>
我想在JUnit测试中做两件事 -
在所有测试之前,删除并重新创建数据库并让它重新迁移.这为每个测试套件创建了一个干净的数据库.
在每次测试之前,清理所有数据库表.在其他框架(例如RSpec/Rails)中,我通过事务运行DB语句来完成此操作,因此它们在测试结束时回滚.不确定Spring MVC世界中的最佳实践是什么.
我不知道从哪里开始实施上述内容,所以任何指导都表示赞赏.
谢谢!
Bar*_*ath 14
首先,您可以在每次测试之前清理数据库,如下所示:
@Autowired
Flyway flyway;
@Before
public void init(){
   flyway.clean();
   flyway.migrate();
}
其次,您可以使用JdbcTestUtils删除表中的所有行.请在此处找到doc:JDBC测试支持 https://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html
您还可以使用@Rollback和@Commit以事务方式运行测试方法
@Rollback指示在测试方法完成后是否应回滚事务测试方法的事务.如果为true,则回滚事务; 否则,交易已提交(另请参阅@Commit).即使没有显式声明@Rollback,Spring TestContext Framework中集成测试的回滚语义也默认为true.
| 归档时间: | 
 | 
| 查看次数: | 4392 次 | 
| 最近记录: |