我正在尝试设置github CI工作流来做我的springboot应用程序测试。但是测试在数据库连接上失败了,尽管在本地它工作得很好。
CI作业如下
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout@v3
- name: Prepare test database
run: docker-compose -f docker-compose-test.yaml up -d
- name: Wait for the database to start
run: wget -qO- https://raw.githubusercontent.com/eficode/wait-for/$WAIT_FOR_VERSION/wait-for | sh -s -- localhost:55001 -- echo "Database is up"
env:
WAIT_FOR_VERSION: 4df3f9262d84cab0039c07bf861045fbb3c20ab7 # v2.2.3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
- name: Make gradlew executable
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew build
Docker组合测试
services:
db-test-database:
image: mysql:5.5
ports:
- "55001:3306"
environment:
- MYSQL_ROOT_PASSWORD=my-secret-pw
- MYSQL_DATABASE=dbTest
- MYSQL_USER=root
- MYSQL_PASSWORD=my-secret-pw
要在测试中配置数据库,我使用projectroot/test/resources/application.yml
spring:
datasource:
url: jdbc:mysql://localhost:55001/dbTest
username: root
password: my-secret-pw
driver-class-name: com.mysql.cj.jdbc.Driver
但是冒烟测试(@SpringBootTest with autowire)失败,表明无法连接到数据库:
AppSmokeTest > contextLoads() FAILED
java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:98
Caused by: org.springframework.beans.factory.BeanCreationException at AbstractAutowireCapableBeanFactory.java:1804
Caused by: javax.persistence.PersistenceException at AbstractEntityManagerFactoryBean.java:421
Caused by: org.hibernate.exception.JDBCConnectionException at SQLStateConversionDelegate.java:112
Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException at SQLError.java:174
Caused by: com.mysql.cj.exceptions.CJCommunicationsException at NativeConstructorAccessorImpl.java:-2
Caused by: java.net.ConnectException at Net.java:-2
谁能告诉我哪里出了问题?
1条答案
按热度按时间n3ipq98p1#
最后,当在CI上运行时,它没有从test/resources/application. yaml中选择用于测试的配置(我是通过使用--debug构建并检查jdbcurl找到的)
解决方案是将配置移动到main/resources/application-test.yaml