-1

I have a simple Spring boot project that can connect to a PostgreSQL Database.

When I install the dependencies with mvn clean install, I get:

org.postgresql.util.PSQLException: The connection attempt failed during Maven install

A "solution" I found was using -DskipTests, but that doesn't solve the issue, it only postpones it.
Because when I do mvn test, I will get the same error.

How can we have a database connection that will not crash tests?


The dependencies are this:

  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
...
    </dependencies>

PS:

The test is the automatically generated one, I think loading the context is what gives the error.

package com.hamza.ince.bonptitcoinapi;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BonPtitCoinApiApplicationTests {

    @Test
    void contextLoads() {
    }
}
  • application.properties:
spring.datasource.url=jdbc:postgresql://postgresql-api:5432/annonce_db?user=annonce_username&password=annonce_password
spring.datasource.username= postgres
spring.datasource.password= password

spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.show_sql=true

spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driver-class-name=org.postgresql.Driver
4
  • How does your test look like, where the connection is happening? Are you sure, that the test configuration is correct and the test can find the database? What error exactly are you getting (which exception)? Commented Oct 12, 2022 at 9:53
  • You are mistaken, it's a test I wrote but a generated test, I agree that I should clarify my question. Commented Oct 12, 2022 at 10:02
  • Well, is your datasource url pointing to a database that is up and running? Commented Oct 12, 2022 at 10:08
  • No, and that's the thing, I can't package my app expecting a database to be up and running, and more importantly, have access to it. For example, if I try to create a Docker image of this app, I can't expect to be connected to database, before I deploy the app. And what about CI/CD, I will not connect to my database, in a pipeline with job for unit test. Commented Oct 12, 2022 at 10:12

1 Answer 1

2

There are multiple solutions possible:

  • Configure a dedicated test environment and use that url in your test profile configuration

  • Use an in-memory database

  • Use a framework like testcontainers to spin up your postgres test-database and pass the url to your application

Here's an example for the last one, use it in the appropriate beforeAll callback of the testing framework.

environment = new DockerComposeContainer<>(new File("path/to/docker-compose.yml"))
  .withLocalCompose(true)
  .withExposedService("postgres_1", 5432);

environment.start();

//build url with
//environment.getServiceHost("postgres_1", 5432)
//environment.getServicePort("postgres_1", 5432)
//pass as datasource via System.setProperty("spring.datasource.url" url)

or without docker-compose

    public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:11.1")
      .withDatabaseName("integration-tests-db")
      .withUsername("sa")
      .withPassword("sa");

// use getters to get host, username, password etc

This of course means whatever is executing your test in your test pipeline must be able to run docker containers.
Otherwise you'd have to go with in-memory db instead of postgres.

Sign up to request clarification or add additional context in comments.

4 Comments

I've heard about Testcontainers, never tried it before, I will check that. Thank you,
For now, I just removed @SpringBootTest annontation, until I've work this through.
@HamzaInce depends on what you want to test. More on the integration side with db, or the repository mocked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.