java Spring Boot:如何在单元测试中使用 liquibase 设置测试数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35997898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot: How to setup test data with liquibase in unit test
提问by René Winkler
I'm trying to setup the database schema and some test data with liquibase for some tests. Each test has a separate changelog which setup the schema and some specific data for the test.
我正在尝试使用 liquibase 为某些测试设置数据库架构和一些测试数据。每个测试都有一个单独的更改日志,用于设置模式和一些特定的测试数据。
In order to make my tests working, I need to drop the schema before each test and fill it with new test data. However, it seems that this is not working because some tests are failing because the old test data is still available. I think something with my configuration is not correct. How can I force liquibase to drop the schema before each test?
为了使我的测试工作,我需要在每次测试之前删除架构并用新的测试数据填充它。然而,这似乎不起作用,因为一些测试失败了,因为旧的测试数据仍然可用。我认为我的配置不正确。如何在每次测试之前强制 liquibase 删除模式?
My tests look as following:
我的测试如下:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyTestConfig.class)
@TestPropertySource(properties = "liquibase.change-log=classpath:changelog/schema-with-testdata.xml")
public class MyRepositoryTest {
The config for the tests looks as follows:
测试的配置如下所示:
@SpringApplicationConfiguration
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.mypackage")
@EntityScan(basePackages = { "com.mypackage.domain" })
@EnableJpaRepositories(basePackages = { "com.mypackage.domain", "com.mypackage.infra.persistence" })
public class MyTestConfig {
And the application.properties under src/main/test/resources is
而 src/main/test/resources 下的 application.properties 是
liquibase.drop-first=true
spring.jpa.hibernate.ddl-auto=none
回答by MemLeak
There is a spring.liquibase.dropFirst
config property. Maybe this is what you're looking for?
有一个spring.liquibase.dropFirst
配置属性。也许这就是你要找的?
回答by user1567291
Not sure if this completely answers your question. There is another property liquibase.default-schema=schemaNameToCreate
不确定这是否完全回答了您的问题。还有另一个属性 liquibase.default-schema=schemaNameToCreate
But even with that I was never able to get it to create the schema from scratch.
但即便如此,我也无法让它从头开始创建模式。