SpringBoot和GitLab CI运行测试

问题描述 投票:3回答:1

我想只使用GitLab CI运行测试,而不是部署我的应用程序。我设法汇编这个.yml文件:

image: java:8

stages:
  - build
  - test

build:
  stage: build
  script: ./gradlew build
  artifacts:
    paths:
      - build/libs/myApp-4.0.0-SNAPSHOT.jar

unitTests:
  stage: test
  script:
    - ./gradlew test

在GitLab管道中,我收到以下错误:

ar.com.sebasira.myApp.myAppApplicationTests> contextLoads FAILED java.lang.IllegalStateException引起:org.springframework.beans.factory.BeanCreationException引起:org.springframework.beans.BeanInstantiationException引起:org.springframework.beans.factory。 BeanCreationException由以下原因引起:org.springframework.beans.BeanInstantiationException由以下引起:org.springframework.boot.autoconfigure.jdbc.DataSourceProperties $ DataSourceBeanCreationException

我猜这可能与数据库有关,对吗?我需要在我的服务器中提供带有凭据的数据库吗?

如果是这样,我该怎么做?我目前正在使用application.properties文件来定义与DB的连接。

还有一个问题......在.gitlab-ci.yml文件中,我需要将路径放到.jar,但每次更新我的应用程序版本时,该文件名都会更改。我需要手动更改吗?

java spring-mvc spring-boot continuous-integration gitlab-ci
1个回答
3
投票

当您使用此测试启动整个Spring上下文时(我认为这是Spring Boot生成的标准测试),注释@RunWith(SpringRunner.class)@SpringBootTest您必须提供数据源。您可以执行以下操作之一:

  • application.properties中的src/test/resources中指定数据库凭据(您可以在数据库服务器上提供测试数据库,因为我不会在每次测试时连接到您的生产数据库)
  • 使用嵌入式H2进行此测试
  • 使用https://www.testcontainers.org/为Spring应用程序测试提供真实而全新的数据库

关于你的GitlabCI问题:无论使用哪个版本,只需使用*匹配任何.jar- build/libs/myApp-*.jar

© www.soinside.com 2019 - 2024. All rights reserved.