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

GitHub workflow中Gradle构建执行测试失败,本地IntelliJ可正常运行

GitHub Actions中Gradle带Test构建失败但本地正常的问题排查与解决

问题背景

执行gradle build -x test时无错误,但带test选项的Gradle构建在GitHub workflow中失败,本地IntelliJ可正常运行。已尝试清除缓存和清理构建,问题未解决。GitHub Actions日志关键错误如下:

  • 无法推断源文件file '/home/runner/work/idau/idau/src/main/java/se/web/ErrorDto.java'的源码根目录。支持的类型为File(仅目录)、DirectoryTree和SourceDirectorySet
  • 构造函数参数2存在未满足的依赖;嵌套异常为org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'personService'的bean时出错
  • java.lang.IllegalStateException: 加载ApplicationContext失败(由ServletTestExecutionListener准备测试实例时触发)

附带当前workflow脚本:

- name: Checkout
    id: step1
    uses: actions/checkout@master
    # with:
      # ref: ${{ github.event.pull_request.head.ref }}
      # repository: ${{github.event.pull_request.head.repo.full_name }}
  
  # Setup JAVA with specific version
  - name: Set up JDK 8
    uses: actions/setup-java@v3
    with:
      cache: gradle
      java-version: '8'
      distribution: 'adopt'
  
  # Setup Gradle with specific version
  - name: Setup Gradle
    uses: gradle/gradle-build-action@v2.3.3
    with:
      gradle-version: 4.10.1
  
 
  - name: Start PostgreSQL on Ubuntu
    run: |
      sudo systemctl start postgresql.service
      pg_isready

  # Create a DB user as per 
  - name: Create additional user
    run: |
      chmod og+X /home/runner/
      sudo -u postgres psql --command="CREATE USER *** PASSWORD '****';" --command="\du"

  # Create database
  - name: Create additional database
    run: |
      sudo -u postgres createdb *** --owner=***;
      sudo -u postgres createdb *** --owner=***;
      sudo -u postgres psql --command="\l"

  # This user is needed as defined in build.gradle
  #invalidating the cache
  - name: Add git user details and perform gradle prerequisite 
    run: | 
        git config --global user.email "${GITHUB_ACTOR}"
        git config --global user.name "${GITHUB_ACTOR}@users.noreply.github.com"
        echo "current user: ${GITHUB_ACTOR}"
        # echo "remove .gradle and generated folder: $(rm -rf .gradle generated)"
        # echo "remove all cached .gradle folder: $(rm -rf ~/.gradle)"
        # echo "give full rights to gradlew : $(chmod +x ./gradlew)"
        
        echo "gradle version: $(gradle -version)"

  - run: gradle clean assemble
  - name: Validate Gradle wrapper
    uses: gradle/wrapper-validation-action@v1

  
  # --no-daemon Ensure no Gradle daemons are running anymore when your workflow completes.
  - name: Build with Gradle
    id: build
    uses: gradle/gradle-build-action@v2.3.3
    with:
      gradle-version: 4.10.1
      arguments: build --info --no-daemon
      
  
  - run: ls -R
 
  - name: Upload build reports
    uses: actions/upload-artifact@v3
    if: success() || failure()
    with:
      name: build-reports
      path: ./build/reports/

错误分析

  1. 源码根目录推断错误:Gradle配置中可能直接指定了单个Java文件作为源码路径,而非目录;或GitHub Actions环境中文件路径/权限存在异常,导致Gradle无法识别源码根目录。
  2. Spring Bean依赖与ApplicationContext加载失败:核心问题是测试环境中Spring上下文无法正常初始化,大概率是数据库连接配置不匹配——GitHub Actions中创建的数据库、用户信息与测试配置文件(如application-test.properties)不一致,导致personService依赖的数据库资源无法获取。
  3. TestExecutionListener异常:属于上述上下文加载失败的连锁反应,无需单独处理,解决根源问题后即可消失。

解决方案

1. 修复源码根目录配置

  • 检查build.gradle中的sourceSets配置,确保仅指定目录而非单个文件。示例正确配置:
    sourceSets {
        main {
            java {
                srcDirs = ['src/main/java']
            }
        }
        test {
            java {
                srcDirs = ['src/test/java']
            }
        }
    }
    
  • 若处理PR请求,需启用checkout步骤中注释的ref和repository配置,确保拉取的是PR分支代码而非默认master分支:
    with:
      ref: ${{ github.event.pull_request.head.ref }}
      repository: ${{github.event.pull_request.head.repo.full_name }}
    

2. 解决Spring测试环境数据库配置问题

  • 验证数据库配置一致性:确保测试配置文件中的数据库URL、用户名、密码与workflow中创建的完全匹配。例如:
    spring.datasource.url=jdbc:postgresql://localhost:5432/<数据库名>
    spring.datasource.username=<创建的用户名>
    spring.datasource.password=<设置的密码>
    
  • 确保数据库服务就绪:在启动PostgreSQL后增加等待逻辑,避免数据库未完全启动就执行后续操作:
    - name: Start PostgreSQL on Ubuntu
      run: |
        sudo systemctl start postgresql.service
        until pg_isready; do sleep 1; done
    
  • 添加数据库连接测试:在创建数据库后新增步骤验证连接,确认数据库可用:
    - name: Test DB connection
      run: |
        PGPASSWORD='<设置的密码>' psql -U <用户名> -d <数据库名> -c "SELECT 1;"
    

3. Gradle环境优化

  • 确保本地与GitHub Actions使用相同版本的Gradle(当前指定4.10.1),避免版本兼容性问题。
  • 在构建前强制清理Gradle缓存,避免缓存干扰:
    - name: Clean Gradle caches
      run: rm -rf ~/.gradle/caches
    
  • 合并重复的Gradle操作,将gradle clean assemble与后续build步骤合并,减少冗余执行:
    - name: Build with Gradle
      id: build
      uses: gradle/gradle-build-action@v2.3.3
      with:
        gradle-version: 4.10.1
        arguments: clean build --info --no-daemon
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 08:45:46