如何使用 spring boot 和 liquibase 在内存数据库中设置集成测试

how to set-up in memory database for IntegrationTest using springboot and liquibase

使用 liquibase 和 springboot,创建​​了 db changelogs 并能够 运行 使用 maven-liquibase-plugin (mvn liquibase:update) 成功。我正在编写集成测试,其中需要以编程方式创建 liquibase 更改。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DummyApplication.class)
@ActiveProfiles("test")
public class TestDummyService
{
    @Autowired
    private SpringLiquibase springLiquibase;
    ...

    @Test
    public void testDummyRequest()
    {
    try {
        Connection connection = springLiquibase.getDataSource().getConnection();
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
        Liquibase liquibase = new liquibase.Liquibase("liquibase/changelog/db-changelog-master.xml", new ClassLoaderResourceAccessor(), database);
        liquibase.update(new Contexts(springLiquibase.getContexts()), new LabelExpression(springLiquibase.getLabels()));
    } catch (LiquibaseException | SQLException e) {
        e.printStackTrace();
    }
}

得到下面的异常而运行进行上面的测试。

java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [liquibase.integration.spring.SpringLiquibase]: Factory method 'liquibase' threw exception; nested exception is java.lang.NoSuchMethodError: liquibase.integration.spring.SpringLiquibase.setLiquibaseSchema(Ljava/lang/String;)V

Caused by: java.lang.NoSuchMethodError: liquibase.integration.spring.SpringLiquibase.setLiquibaseSchema(Ljava/lang/String;)

下面是应用程序-test.property文件。

  #NOTE : spring.liquibase is the working one .

  liquibase.changeLog=classpath:liquibase/changelog/db-changelog-master.xml
  liquibase.enabled=true
  liquibase.url=jdbc:h2:mem:cpass;DB_CLOSE_DELAY=-1
  liquibase.user=root
  liquibase.password=
  spring.liquibase.dropFirst=true
  hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  jdbc.driverClassName=org.h2.Driver
  spring.liquibase.enabled=true
  spring.liquibase.change-log=classpath:liquibase/changelog/db-changelog-master.xml
  #spring.liquibase.driver=com.mysql.jdbc.Driver
  spring.liquibase.url=jdbc:mysql://localhost:3306/dummy
  spring.liquibase.user=root
  spring.liquibase.password=

**pom.xml : **

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.1.3.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.1.3.RELEASE</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
        <version>3.4.2</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.1.3.RELEASE</version>
        </plugin>

        <plugin>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>3.4.2</version>
            <configuration>
                <propertyFile>liquibase/liquibase.properties</propertyFile>
                <changeLogFile>liquibase/changelog/db-changelog-master.xml</changeLogFile>
            </configuration>
        </plugin>
    </plugins>
</build>

我在测试中是否遗漏了什么 class 或任何示例项目 url 也很有帮助。我是 springboot 和 liquibase 的新手。

Spring Boot 2.1.3 依赖于 Liquibase 3.6.3,但您在 pom.xml.

中指定了 3.4.2

话虽如此,SpringBoot 的 Liquibase 自动配置将执行您在单元测试中自己编写的所有内容,因为您已经创建了用于激活自动配置的属性。您可以删除测试方法中的所有代码,而只是将 @Autowire 数据源添加到测试 class 中。 Spring 将为您应用 Liquibase 变更日志。

最后一件事:如果可以的话,最好在 pom.xml 中添加一个 <parent>,这意味着您不需要指定每个依赖项的各个版本(包括 Liquibase ). Spring Boot 有很多依赖项,升级时要保持版本同步需要做很多工作。这也可以防止您的 NoSuchMethodError。