springboot应用程序中单元和集成测试的并行化

时间:2020-02-06 14:52:47

标签: java spring-boot maven junit maven-surefire-plugin

我正在尝试在我的Spring Boot应用程序中并行化测试。为此,我在maven surefire插件中进行了更改,以将并行参数(以类为值)和线程计数参数(以5为值)包括在内。但是,当我运行maven全新安装时,mvn clean install会出现PessimisticLockingFailure或Pessimistic Exception。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <parallel>classes</parallel>
        <threadCount>5</threadCount>
    </configuration>
</plugin>

对于我的测试用例,我已经在测试文件夹下的应用程序属性文件中配置了H2数据库。在Google上阅读后,我发现我需要在数据库URL中包含MVCC=TRUE;DEFAULT_LOCK_TIMEOUT=10000。因此,因此应用程序属性文件具有以下配置:

spring:
    datasource:
        type: com.zaxxer.hikari.HikariDataSource
        url: jdbc:h2:TestDB;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MVCC=TRUE;DEFAULT_LOCK_TIMEOUT=10000
    jpa:
        database-platform: io.github.jhipster.domain.util.FixedH2Dialect
        database: H2
        open-in-view: false
        show-sql: true
        hibernate:
            ddl-auto: none
            naming:
                physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
                implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
        properties:
            hibernate.id.new_generator_mappings: true
            hibernate.connection.provider_disables_autocommit: true
            hibernate.cache.use_second_level_cache: false
            hibernate.cache.use_query_cache: false
            hibernate.generate_statistics: false
            hibernate.hbm2ddl.auto: validate
            hibernate.jdbc.time_zone: UTC

现在我了解到,此悲观锁已完成,因为它应该是为了遵循ACID属性。我还了解到,在并发的情况下,H2使用悲观锁定而不是乐观锁定,这很有意义,因为我们不想破坏数据。但是,然后如何并行化我的测试。我还看到执行这些测试时,maven使用了不同的线程,因为线程名称已打印出来,但随后出现此悲观异常。

Caused by: org.hibernate.PessimisticLockException: could not execute statement
    at com.x.y.z.repository.a.ProductionDataRepositoryTest.setUp(ProductionDataRepositoryTest.java:45)
Caused by: org.h2.jdbc.JdbcSQLException: 
Timeout trying to lock table ; SQL statement:
insert into my_table

我想到的一个想法是,也许我们应该为每个线程运行单独的h2实例,如果有4个线程,则应该有4个h2实例,但是那不是很昂贵,如果不是,那么怎么办那? 或者在Spring Boot,java和maven中实现测试并行化还有其他方法。

建议。

0 个答案:

没有答案