You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JHipster项目测试文件配置:解决生产构建测试报错问题

解决HibernateTimeZoneIT测试加载ApplicationContext失败的问题

1. 统一测试环境时区配置

在src/test/resources/config/application-test.yml(或对应properties文件)中明确指定时区,和测试用例预期的GMT保持一致:

spring:
  jackson:
    time-zone: GMT
  jpa:
    properties:
      hibernate:
        jdbc:
          time_zone: GMT
  datasource:
    url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;TIME_ZONE=GMT

如果用properties格式:

spring.jackson.time-zone=GMT
spring.jpa.properties.hibernate.jdbc.time_zone=GMT
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;TIME_ZONE=GMT

2. 确保测试依赖兼容且完整

检查pom.xml中的测试依赖,避免版本冲突:

  • 确认spring-boot-starter-test依赖正确引入,排除过时的JUnit引擎:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>
  • 若用到Hibernate测试工具,保证版本和项目中Hibernate主版本一致:
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-testing</artifactId>
    <scope>test</scope>
</dependency>

3. 修正测试类的上下文配置

检查HibernateTimeZoneIT类的注解配置,确保指向正确的启动类和测试配置:

@SpringBootTest(classes = YourApplicationMainClass.class)
@TestPropertySource(locations = "classpath:config/application-test.yml")
public class HibernateTimeZoneIT {
    // 原有测试代码
}

替换YourApplicationMainClass为项目实际的Spring Boot启动类。

4. 清理缓存并分步验证

  • 先清理构建缓存:
./mvnw clean
  • 单独运行故障测试类,确认修复效果:
./mvnw -Pprod test -Dtest=HibernateTimeZoneIT
  • 测试通过后再执行完整构建:
./mvnw -Pprod clean verify

5. 排查依赖冲突

如果以上步骤无效,生成依赖树检查冲突:

./mvnw dependency:tree

找到Hibernate、Spring相关的版本冲突依赖,通过<exclusions>标签排除不兼容的版本。


内容的提问来源于stack exchange,提问作者Hadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 10:12:11