在Mockito单元测试期间如何生成H2嵌入式数据库?

时间:2019-05-24 10:12:20

标签: java spring-boot junit h2

我目前正在使用springboot API,已确定我和我的同事将在开发期间使用H2嵌入式数据库。

启动应用程序后,数据库可以正常运行,但是在单元测试期间我无法使其正常工作:它们都失败了org.h2.jdbc.JdbcSQLException: Table '[...]' not found

我基本上没有编写大部分代码,如果需要更多内容,请告诉我:

application.properties文件,用于初始化H2数据库:

datasources.member-request.url=jdbc:h2:mem:db;IGNORECASE=TRUE;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;INIT=CREATE SCHEMA IF NOT EXISTS DMDEOWNER\\;SET SCHEMA DMDEOWNER\\;runscript from 'classpath:h2_init_script.sql'
datasources.member-request.username=sa
datasources.member-request.password=
datasources.member-request.driver-class-name=org.h2.Driver
datasources.member-request.initialize=true

如您在上面看到的,我们在H2的初始化处执行一个名为h2_init_script.sql的SQL脚本,在此文件中,我们有一些这样的SQL语句(例如,我将向您显示T_THEME表)

/* CREATE statements */
CREATE TABLE DMDEOWNER.T_THEME (
    F_NUM_THEME NUMBER(14) NOT NULL,
    F_REF_THEME VARCHAR2(30) NOT NULL,
    F_LIB_THEME VARCHAR2(50) NOT NULL,
    F_POS_THEME NUMBER(4) NOT NULL,
    F_BOOL_ACTIF NUMBER(1) NOT NULL,
    F_UTILISATEUR_MAJ VARCHAR2(40) NOT NULL,
    F_DT_CREATION DATE NOT NULL,
    F_DT_MAJ DATE,
    PRIMARY KEY (F_NUM_THEME)
);

/* INSERT statements */
/* ... */

这是我的测试课程(及其父级):

public class ThemeRepositoryTest extends AbstractRepositoryTest {

  private final static int NB_ACTIVE_ENTITIES = 2;
  private final static int NB_INACTIVE_ENTITIES = 1;

  @Autowired
  private ThemeRepository repository;

  @Autowired
  private ThemeMapper mapper;


  // -------------------------------------------------------------------------
  // CREATE
  // -------------------------------------------------------------------------
  @Before
  public void init() {
    for (int i = INTEGER_ZERO; i < NB_ACTIVE_ENTITIES; i++) {
      insertTheme(true);
    }
    for (int i = INTEGER_ZERO; i < NB_INACTIVE_ENTITIES; i++) {
      insertTheme(false);
    }
    entityManager.flush();
  }

  // -------------------------------------------------------------------------
  // READ
  // -------------------------------------------------------------------------
  @Test
  public void findAll() {
    List<ThemeEntity> list = repository.findAll();
    assertEquals(NB_ACTIVE_ENTITIES + NB_INACTIVE_ENTITIES, list.size());
  }
}

@RunWith(SpringRunner.class)
@DataJpaTest
public abstract class AbstractRepositoryTest {

  @Autowired
  protected TestEntityManager entityManager;

  protected ThemeEntity insertTheme(boolean active) {
    ThemeEntity entity =  newThemeEntity(3L, "NAME", active, "USER");
    return entityManager.merge(entity);
  }
}

这是我发生错误的地方,当我调用init()时,在insertTheme()函数中:

findAll(io.lacipav.repository.ThemeRepositoryTest)  Time elapsed: 0.227 s  <<< ERROR!
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement
    at io.lacipav.repository.ThemeRepositoryTest.init(ThemeRepositoryTest.java:38)
Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement
    at io.lacipav.repository.ThemeRepositoryTest.init(ThemeRepositoryTest.java:38)
Caused by: org.h2.jdbc.JdbcSQLException: 
Table "T_THEME" not found; SQL statement:
select themeentit0_.f_num_theme as f_num_th1_11_0_, themeentit0_.f_bool_actif as f_bool_a2_11_0_, themeentit0_.f_dt_creation as f_dt_cre3_11_0_, themeentit0_.f_dt_maj as f_dt_maj4_11_0_, themeentit0_.f_lib_theme as f_lib_th5_11_0_, themeentit0_.f_pos_theme as f_pos_th6_11_0_, themeentit0_.f_ref_theme as f_ref_th7_11_0_, themeentit0_.f_utilisateur_maj as f_utilis8_11_0_ from t_theme themeentit0_ where themeentit0_.f_num_theme=? [42102-197]
    at io.lacipav.repository.ThemeRepositoryTest.init(ThemeRepositoryTest.java:38)

由于错误告诉我它找不到我的表,所以我猜测是因为在构建.war文件时我的数据库未运行,如果可以的话如何初始化它更快?

编辑: 根据要求,这是T_THEME的Entity类:

@Entity
@Getter
@Setter
@Table(name = "T_THEME")
public class ThemeEntity {

  /** Primary key of the table. */
  @Id
  @NotNull
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "theme_generator")
  @SequenceGenerator(name = "theme_generator", sequenceName = "SEQ_NUM_THEME", allocationSize = 1)
  @Column(name = "f_num_theme")
  private Long idTechnique;

  /** Theme functional identifier */
  @NotNull
  @Column(name = "f_ref_theme")
  private String reference;

  /** Theme simple short length label */
  @NotNull
  @Column(name = "f_lib_theme")
  private String name;

  //...
}

注意:将生成SQL序列“ SEQ_NUM_THEME”,并将其与SQL脚本中的F_NUM_THEME关联。

编辑2:

正如Lesiak所指出的那样,我的错误可能是由于我的数据源被注解@DataJpaTest所代替。通过仔细查看日志,我发现了一些有趣的行。

[INFO ] 2019-05-24 16:22:20.341 [main] org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceBeanFactoryPostProcessor.process(TestDatabaseAutoConfiguration.java:106) : Replacing 'dataSource' DataSource bean with embedded version
[INFO ] 2019-05-24 16:22:20.346 [main] org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:824) : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] with [Root bean: class [org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
[INFO ] 2019-05-24 16:22:21.050 [main] org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory.initDatabase(EmbeddedDatabaseFactory.java:189) : Starting embedded database: url='jdbc:h2:mem:4ef27020-c472-4be9-a9af-73f5e0175846;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'

我们可以在最后一行看到我的网址连接jdbc:h2:mem:db已被jdbc:h2:mem:4ef27020-c472-4be9-a9af-73f5e0175846取代。这可能就是为什么我遇到一些Table not found错误。

2 个答案:

答案 0 :(得分:1)

问题出在您使用的@DataJpaTest上。 参见Documentation of @DataJpaTest

  

默认情况下,带有@DataJpaTest注释的测试将使用嵌入式内存数据库(替换任何显式或通常自动配置的DataSource)。 @AutoConfigureTestDatabase批注可用于覆盖这些设置。

您会在日志中找到类似的内容

EmbeddedDataSourceBeanFactoryPostProcessor : Replacing 'dataSource' DataSource bean with embedded version

要修复,请使用:

spring.test.database.replace=none
spring.jpa.properties.hibernate.default_schema=DMDEOWNER 

答案 1 :(得分:0)

我最终尝试了很多事情,所以我不确定为什么它现在可以工作...

the documentation中,据说默认情况下它将尝试找到schema.sql来创建数据库,并尝试data.sql来填充它。我当时使用的是自己的.sql文件,可以同时完成这两项工作,因此我按照建议将其分开,并且开始工作!

所以现在我的application.properties如下:

# Datasource connection settings to H2 in-memory database
# To init db with h2_init_script.sql add the following to the url : INIT=CREATE SCHEMA IF NOT EXISTS DMDEOWNER\\;SET SCHEMA DMDEOWNER\\;runscript from 'classpath:h2_init_script.sql'
datasources.member-request.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;IGNORECASE=TRUE;
datasources.member-request.username=sa
datasources.member-request.password=
datasources.member-request.driver-class-name=org.h2.Driver
datasources.member-request.initialize=true

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.web-allow-others=true

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

由于我不再在application.properties中创建架构,因此将其添加到schema.sql文件中:

CREATE SCHEMA IF NOT EXISTS DMDEOWNER;
SET SCHEMA DMDEOWNER;

最后,我在Entity类的序列声明中添加了架构的名称前缀,因为我遇到了一些SEQUENCE NOT FOUND错误:

@Entity
@Getter
@Setter
@Table(name = "T_THEME")
public class ThemeEntity {

  /** Primary key of the table. */
  @Id
  @NotNull
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "theme_generator")
  @SequenceGenerator(name = "theme_generator", sequenceName = "DMDEOWNER.SEQ_NUM_THEME", allocationSize = 1)
  @Column(name = "f_num_theme")
  private Long idTechnique;

  //...
}