H2数据库表无法为DataJpaTests创建。

huangapple go评论59阅读模式
英文:

H2 Database Tables can't be created for DataJpaTests

问题

我的Springboot项目使用MariaDB,并且在正常模式下JPA运行良好。
当我尝试运行我的DataJpa JUNIT测试时,出现了错误。"找不到表Company(数据库为空)"。

这是我的正常数据库的application.properties文件。

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mariadb://localhost:3306/app
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.show-sql= true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.properties.hibernate.format_sql=true
spring.data.rest.base-path=/app

这是我的测试数据库的application-test.properties文件。

# 必须实际命名为application-test.properties ;-)
# 启用H2控制台
spring.h2.console.enabled=true

# H2控制台路径
spring.h2.console.path=/h2-console

# 配置H2数据库
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=

# 配置JPA和Hibernate
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true

这是我的JUNIT测试。

package ch.wiss.unternehmensliste.repository;

import ch.wiss.unternehmensliste.model.Company;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.annotation.Rollback;

import java.util.List;
import java.util.Optional;

@DataJpaTest
class CompanyRepositoryTest {
    @Autowired
    private TestEntityManager entityManager;
    @Autowired
    CompanyRepository companyRepository;

    /**
     * 保存公司的JUnit测试
     */

    @Test
    public void saveCompanyTest(){
        Company company = new Company("Microsoft", "www.microsoft.com", "Zürich");
        companyRepository.save(company);

        Assertions.assertThat(company.getId()).isGreaterThan(0);
    }
    
}
英文:

My Springboot project uses MariaDB and JPA works fine in normal mode.
When I tried to run my DataJpa JUNIT Tests, there was an error. "The Table Company could not be found (the database is empty)".

This is my application.properties File for my normal database.

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mariadb://localhost:3306/app
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.show-sql= true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.properties.hibernate.format_sql=true
spring.data.rest.base-path=/app

This is my application-test.properties File for my test database.

# must actually be named application-test.properties ;-)
# Enable H2 console
spring.h2.console.enabled=true

# H2 console path
spring.h2.console.path=/h2-console

# Configure H2 database
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=

# Configure JPA and Hibernate
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true

These are my JUNIT Tests.

package ch.wiss.unternehmensliste.repository;

import ch.wiss.unternehmensliste.model.Company;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.annotation.Rollback;

import java.util.List;
import java.util.Optional;

@DataJpaTest
class CompanyRepositoryTest {
    @Autowired
    private TestEntityManager entityManager;
    @Autowired
    CompanyRepository companyRepository;

    /**
     * JUnit test for saving a Company
     */

    @Test
    public void saveCompanyTest(){
        Company company = new Company("Microsoft", "www.microsoft.com", "Zürich");
        companyRepository.save(company);

        Assertions.assertThat(company.getId()).isGreaterThan(0);
    }
    
}

答案1

得分: 0

以下是翻译好的部分:

在 application.properties 中的配置 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect 导致 DataJPATest 框架使用 engine=InnoDB 扩展来创建表格。H2 无法处理这种情况,无法创建所需的表格,因此会出现错误消息 "Table not found, database empty"。最初的错误只在完整测试输出的顶部可见。

只需取消注释应用程序属性中的这行 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect。在我的情况下,测试和常规应用都可以在没有这行的情况下运行。

英文:

The configuration spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect in the application.properties causes the DataJPATest framework to create the tables with engine=InnoDB extension. H2 cannot handle this and fails to create the required tables, therefore the error message "Table not found, database empty". The initial error is only visible on top of the full test output.

Just uncomment the line spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect in the application properties. In my case, both test and regular app work without that line.

huangapple
  • 本文由 发表于 2023年4月19日 15:43:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定