gpt4 book ai didi

java - Mockito单元测试时如何生成H2嵌入式数据库?

转载 作者:行者123 更新时间:2023-12-02 09:51:34 30 4
gpt4 key购买 nike

我目前正在开发 springboot API,我和我的同事决定在开发过程中使用 H2 嵌入式数据库。

数据库在启动应用程序时可以正常生成,但我无法使其在单元测试期间工作:它们都失败,并显示 org.h2.jdbc.JdbcSQLException: Table '[...]' not找到了

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

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);
}
}

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

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
@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 指出的,我的错误可能来自于我的 DataSource 被注释 @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'

我们可以在最后一行看到我的url连接jdbc:h2:mem:db已被jdbc:h2:mem:4ef27020-c472-4be9-a9af-73f5e0175846替换。这可能就是为什么我收到一些 Table not found 错误的原因。

最佳答案

问题在于您正在使用的@DataJpaTest。请参阅Documentation of @DataJpaTest

By default, tests annotated with @DataJpaTest will use an embedded in-memory database (replacing any explicit or usually auto-configured DataSource). The @AutoConfigureTestDatabase annotation can be used to override these settings.

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

EmbeddedDataSourceBeanFactoryPostProcessor : Replacing 'dataSource' DataSource bean with embedded version

要修复,请使用:

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

关于java - Mockito单元测试时如何生成H2嵌入式数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56290532/

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