简体   繁体   English

使用 H2 数据库和 Liquibase 配置 Spring Boot

[英]Configure Spring boot with H2 database and Liquibase

I am using Spring boot2, I am trying to configure a Unit Test using H2 + Liquibase + JUNIT.我正在使用 Spring boot2,我正在尝试使用 H2 + Liquibase + JUNIT 配置单元测试。

I think that liquibase is not executing the changeLog files and apply the SQL Commands, the unit test does not recognized my tables.我认为 liquibase 没有执行 changeLog 文件并应用 SQL 命令,单元测试无法识别我的表。

I put wrongs sql in my file to see if the changelog files are executed, but, seems that is not being executing.我在我的文件中放了错误的 sql 以查看更改日志文件是否已执行,但是似乎没有执行。

Why my application can not access table?为什么我的应用程序无法访问表? Maybe liquibase is not executing?也许 liquibase 没有执行?

in my src/test/resource I have this file: application.yml在我的src/test/resource我有这个文件: application.yml

spring:
  application:
    name: my-api
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:myDB;MODE=MySQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    username: sa
    password: sags
  liquibase:
    enabled: true
    user: sa
    password: sags
    change-log: classpath:/db/changelog/db.changelog-master.xml
  jpa:
    hibernate:
      ddl-auto: none
      database-platform: org.hibernate.dialect.H2Dialect
    show-sql: true
    properties:
      hibernate:
        use_sql_comments: true
        format_sql: true
  h2:
    console:
      enabled: true
      path: /console

My Test class:我的测试课:

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class FooRepositoryTest {

    @Autowired
    private FooRepository fooRepository;

    @Test
    public void findAllMustReturnAnything() {
        List<Object> result = fooRepository.tables();
    }
}

The FooRepository method: FooRepository 方法:

@Query(value = "SHOW TABLES", nativeQuery = true)
    List<Object> tables();

When I run my Unit Test I have the follow result:当我运行单元测试时,我得到以下结果:

在此处输入图像描述

My changelog file is in : src/main/resources/db我的变更日志文件位于: src/main/resources/db

UPDATE: I found why liquibase was not executing my sql codes.更新:我发现为什么 liquibase 没有执行我的 sql 代码。 It was because my SQL files had "dbms:mysql", so, how I am executing with H2 liquibase will not apply.这是因为我的 SQL 文件有“dbms:mysql”,所以,我使用 H2 liquibase 执行的方式将不适用。

-- changeset 1.0:2 dbms:mysql
command
-- rollback command

Now, I need to know why my selected database in the session is not myDB.现在,我需要知道为什么我在会话中选择的数据库不是 myDB。

I think in you case Spring turns on auto configure datasource.( https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/api/org/springframework/boot/test/autoconfigure/orm/jpa/AutoConfigureTestDatabase.Replace.html )我认为在您的情况下,Spring 会打开自动配置数据源。( https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/api/org/springframework/boot/test/autoconfigure/orm/jpa /AutoConfigureTestDatabase.Replace.html )

Try to turn it off.尝试将其关闭。

spring.test.database.replace=none

And there is some unnecessary configuration.还有一些不必要的配置。

  liquibase:
    user: sa
    password: sags

You can remove user&password, liquibase use it from datasource.您可以删除用户和密码,liquibase 从数据源中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM