Search code examples
dockerkotlingradle

Gradle dockerfile for springboot project error when I run test


Here's my error:

ApplicationTests > contextLoads() FAILED
80.30     java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:180
80.30         Caused by: org.springframework.beans.factory.BeanCreationException at AbstractBeanFactory.java:326
80.30             Caused by: org.springframework.beans.factory.BeanCreationException at AbstractAutowireCapableBeanFactory.java:1788
80.30                 Caused by: liquibase.exception.UnexpectedLiquibaseException at SpringLiquibase.java:259
80.30                     Caused by: liquibase.exception.DatabaseException at SpringLiquibase.java:251
80.30                         Caused by: org.postgresql.util.PSQLException at ConnectionFactoryImpl.java:346
80.30                             Caused by: java.net.ConnectException at Net.java:-2

Here's my dockerfile:

FROM gradle:8-jdk17 AS build
WORKDIR /app
COPY build.gradle.kts ./
COPY settings.gradle.kts ./
COPY settings.gradle.kts ./
#COPY gradle ./gradle
COPY src ./src
RUN gradle build --no-daemon

FROM openjdk:17-jdk-slim
WORKDIR /app
COPY --from=build /app/build/libs/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

When I build my dockerfile I get the above error. And here's my build.gradle.kts if you want to see it:

plugins {
    id("org.springframework.boot") version "3.3.0"
    id("io.spring.dependency-management") version "1.1.5"
    kotlin("plugin.jpa") version "1.9.24"
    kotlin("jvm") version "1.9.24"
    kotlin("plugin.spring") version "1.9.24"
}

group = "com.delivery"
version = "0.0.1-SNAPSHOT"

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.liquibase:liquibase-core")
    implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("com.google.firebase:firebase-admin:9.3.0")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    implementation("org.springframework.boot:spring-boot-starter-amqp")
    runtimeOnly("org.postgresql:postgresql")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
    testImplementation("org.springframework.security:spring-security-test")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

kotlin {
    compilerOptions {
        freeCompilerArgs.addAll("-Xjsr305=strict")
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

Any help would be appreciated. The bizarre thing is I only have an empty test class that came with spring initializer when first generated the project


Solution

  • The error tells you

    1. Liquibase - which is tool that manages the schema of a database is trying to connect to a database, and
    2. That database is Postgres, but the driver cannot connect

    Do you have a Postgres server running? and if you so have you defined the connection string?

    If you have a local Postgres note that because you are running the application inside Docker, a local Postgres server cannot be access from localhost but must use an IP address to get out of the Docker container.

    If you never even intended to have a database, then remove the dependencies of Liquibase and Postgres.