java 如何在GitHub Action上运行Sping Boot 应用的单元测试

omhiaaxx  于 2023-02-14  发布在  Java
关注(0)|答案(2)|浏览(129)

我有一个应用程序,基本上做CRUD。我可以运行我的单元测试本地,但在CI(GitHub行动)失败。我得到的错误,因为PostgreSQL。在这里你可以看到错误。我不能修复它。你可以访问这个LINK上的整个仓库。你可以看到我的ci.yaml文件如下;

name: CI

on:
  pull_request:
  push:
    branches: [develop, main]

concurrency:
  group: ci-${{ github.ref }}-group
  cancel-in-progress: true

jobs:
  default:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
      - name: Build with Maven
        run: mvn -B package --file pom.xml
      - name: Update dependency graph
        uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
      - name: Build Jar file
        run: ./project-dev build-jar
      - name: Save Jar file
        uses: actions/upload-artifact@v3
        with:
          name: demo-0.0.1-SNAPSHOT
          path: target/demo-0.0.1-SNAPSHOT.jar
          retention-days: 1

有人能帮我在CI上运行单元测试吗?

pobjuy32

pobjuy321#

您需要确保数据库运行。
您的程序期望在localhost:5432下有一个名为school_management的Posgres DB。
但是,这样的数据库在脚本中不可用。
要设置数据库,可以使用this one等现有操作:

steps:
- uses: harmon758/postgresql-action@v1
  with:
    postgresql version: '11'
    postgresql db: school_management
    postgresql user: learning
    postgresql password: sa123456

或者,您可以使用PosgreSQL服务容器,如here所述:

# Service containers to run with `container-job`
    services:
      # Label used to access the service container
      postgres:
        # Docker Hub image
        image: postgres
        # Provide the password for postgres
        env:
          POSTGRES_PASSWORD: sa123456
          POSTGRES_USER: learning
          POSTGRES_DB: school_management
        # Set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

然而,这会使它使用不同的主机名运行,因此您必须将您的spring.datasource.url更改为jdbc:postgresql://localhost:5432/school_management或类似的主机名。
集成在您的工作流程中,它可能如下所示:

name: CI

on:
  pull_request:
  push:
    branches: [develop, main]

concurrency:
  group: ci-${{ github.ref }}-group
  cancel-in-progress: true

jobs:
  default:
    runs-on: ubuntu-latest
        # Service containers to run with `container-job`
    services:
      # Label used to access the service container
      postgres:
        # Docker Hub image
        image: postgres
        # Provide the password for postgres
        env:
          POSTGRES_PASSWORD: sa123456
          POSTGRES_USER: learning
          POSTGRES_DB: school_management
        # Set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
      # override spring.datasource.url
      - name: Setup config
        run: |
          mkdir config
          echo 'spring.datasource.url=jdbc:postgresql://postgres:5432/school_management' > config/application.properties
      - name: Build with Maven
        run: mvn -B package --file pom.xml
      - name: Update dependency graph
        uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
      - name: Build Jar file
        run: ./project-dev build-jar
      - name: Save Jar file
        uses: actions/upload-artifact@v3
        with:
          name: demo-0.0.1-SNAPSHOT
          path: target/demo-0.0.1-SNAPSHOT.jar 
          retention-days: 1
yi0zb3m4

yi0zb3m42#

看看你的日志第1351行
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
您的测试正在尝试连接到一个不可用的本地Postgres示例。另外,请查看您的测试,您同时具有单元测试和集成测试。而集成测试需要加载应用程序上下文,这意味着您在管道内部运行的应用程序将无法连接到Postgres。因此,所有使用Postgres的集成测试都将失败。
但是,您的其他测试都通过了,第2085行:
2023-02-14 12:13:39.378 INFO 1740 --- [ main] o.s.j.d.e.EmbeddedDatabaseFactory : Starting embedded database: url='jdbc:h2:mem:d00124ab-b172-4fd1-bf29-b4836ae2f938;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
这些都是工作的,因为您的应用程序正确连接到您拥有的h2数据库。
StudentRepositoryTest正在工作,因为您的类@DataJpaTest中有以下注解,它将 Boot 此集成测试并连接到内存中的数据库。
我认为失败的测试是以下DemoApplicationTests

@SpringBootTest
class DemoApplicationTests {

    @Test
    void contextLoads() {
    }

}

由于此测试加载了应用程序上下文(整个应用程序),并将自动尝试与postgres连接。
所以要解决这个问题,只需删除文件。或者我推荐的更好的解决方案(更高级一点)是使用名为testcontainers的东西,并在容器内实际运行postgres数据库。
我建议使用后一种解决方案的原因通常是,一旦您想要运行集成测试,您就会尝试获得应用程序在生产环境中运行的精确解决方案。

相关问题