首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用H2数据库SpringBoot运行Junit测试

无法使用H2数据库SpringBoot运行Junit测试
EN

Stack Overflow用户
提问于 2022-01-24 21:43:39
回答 1查看 2K关注 0票数 0

我的单元测试有个奇怪的问题。我的目标是在H2数据库上运行测试,而不是在Mysql数据库上运行。

实际上,奇怪的是,当我单击maven测试时,它运行应用程序,试图连接到mysql..崩溃和开始与h2数据库和单元测试失败。

代码语言:javascript
运行
复制
@ExtendWith(SpringExtension.class)
@SpringBootTest

// @ActiveProfiles("test") // Without this it runs on my mysql and works.. , 
// with this annotation the behaviour is described above

@Transactional
class BelugaprojectsApplicationTests {

    @Autowired
    private ICheckConfigService iCheckConfigService;

    @Autowired
    private CheckConfigJpaRepository checkConfigJpaRepository;

    @Test 
    void getAllCheckConfigDeploiement() {
        assertThat(iCheckConfigService.getAllCheckConfigDeploiement(
                Integer.parseInt(AppConstants.DEFAULT_PAGE_NUMBER), 
                Integer.parseInt(AppConstants.DEFAULT_PAGE_SIZE),
                "id").getTotalElements() > 0);
    }
}

属性

代码语言:javascript
运行
复制
  datasource:
    url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
    username: root
    password:
  jpa:
    open-in-view: true
    hibernate:
      ddl-auto: create-drop

在启动测试配置文件时运行它抛出的

代码语言:javascript
运行
复制
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "HISTORIQUEDEPLOIEMENT" non trouvée
Table "HISTORIQUEDEPLOIEMENT" not found; SQL statement:
alter table historiquedeploiement add constraint FK134pkswiisobg18okjr9pegt7 foreign key (id_namespace) references namespace (id) [42102-200]
Table "CHECKCONFIGDEPLOIEMENT" non trouvée
Table "CHECKCONFIGDEPLOIEMENT" not found; SQL statement:

依赖关系

代码语言:javascript
运行
复制
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
<dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
EN

回答 1

Stack Overflow用户

发布于 2022-01-24 21:57:00

尝试使用此h2文件配置:

代码语言:javascript
运行
复制
    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(basePackages = "com.example.repository")
    public class DatabaseConfig {
       @Autowired
       private DataSource dataSource;
       @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new 
        LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource);
        em.setPackagesToScan(new String[] { "com.example.model" });
        em.setPersistenceUnitName("entityManager");

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());

        return em;
    }
    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }
    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "create");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        return properties;
    }    
}

然后像这样修改application.properties:

数据源配置

代码语言:javascript
运行
复制
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:MyProjectName;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
spring.application.admin.enabled=true

添加此依赖项:

代码语言:javascript
运行
复制
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.200</version>
</dependency>

对我来说很管用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70840879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档