【问题标题】:Not able to run Junit test with H2 database SpringBoot无法使用 H2 数据库 Spring Boot 运行 Junit 测试
【发布时间】:2022-01-24 21:43:39
【问题描述】:

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

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

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

属性

  datasource:
    url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
    username: root
    password:
  jpa:
    open-in-view: true
    hibernate:
      ddl-auto: create-drop

在激活测试配置文件的情况下运行它会抛出

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:

依赖关系

<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>

【问题讨论】:

  • 属性文件的名称是什么,它在哪里?
  • application-test.yaml 位于 test/resources

标签: java spring-boot junit h2 junit5


【解决方案1】:

尝试使用这个 h2 文件配置:

    @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 :

数据源配置

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

添加这个依赖:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.200</version>
</dependency>

它对我有用

【讨论】:

  • 对不起,但这根本不需要这样做。我会等待更好的解决方案
  • 你没有添加 h2 依赖,它不会在你的项目中工作
  • 是的,sry 添加了
猜你喜欢
  • 2021-09-05
  • 2019-02-02
  • 2019-05-17
  • 2018-09-12
  • 2017-08-01
  • 2015-11-20
  • 2013-07-13
  • 1970-01-01
  • 2017-09-08
相关资源
最近更新 更多