gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:01:42 25 4
gpt4 key购买 nike

使用 liquibase 和 springboot 创建数据库更改日志,并能够使用 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;)

下面是application-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>

测试类中是否缺少任何内容或任何示例项目 url 也很有帮助。我是 springboot 和 liquibase 的新手。

最佳答案

Spring Boot 2.1.3 依赖于 Liquibase 3.6.3,但您已在 pom.xml 中指定了 3.4.2。

话虽如此,Spring Boot 的 Liquibase 自动配置将执行您在单元测试中自己编写的所有操作,因为您已经创建了用于激活自动配置的属性。您可以删除测试方法中的所有代码,然后将 DataSource @Autowire 到测试类中。 Spring 将为您应用 Liquibase 变更日志。

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

关于java - 如何使用 spring boot 和 liquibase 在内存数据库中设置集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54850041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com