1
resposta

erro ao rodar o mvn clean package

quando rodo o: mvn clean package, parece que nao consegue conectar com a database, mas tenho o codigo igual ao da aula.

me da esse erro:

[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   LogsApplicationTests.contextLoads » IllegalState Failed to load ApplicationCon...
[INFO] 
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  14.263 s
[INFO] Finished at: 2025-05-17T15:53:40+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project logs: There are test failures.
[ERROR] 
[ERROR] Please refer to /home/pc/Downloads/27-03/Projeto inicial/grafana-loki/logs/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

application.properties

server.port=8080

# postgres
spring.datasource.url=jdbc:postgresql://database-api-cursos:5432/logsdb
spring.datasource.username=alura
spring.datasource.password=nJ6vsW37rCd1v88
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

# actuator
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=health,info,metrics,prometheus

# prometheus
management.metrics.enable.jvm=true
management.metrics.export.prometheus.enabled=true
management.metrics.distribution.slo.http=50ms,100ms,200ms,300ms,500ms,1s
management.metrics.tags.application=api-cursos

# project
info.app.name=@project.name@
info.app.description=@project.description@
info.app.version=@project.version@
info.app.encoding=@project.build.sourceEncoding@
1 resposta

Olá!

Esse erro acontece porque os testes automatizados estão tentando iniciar a aplicação, mas ela não consegue se conectar ao banco de dados PostgreSQL no momento da execução do comando mvn clean package.

###O problema:

No seu application.properties a URL do banco é:

spring.datasource.url=jdbc:postgresql://database-api-cursos:5432/logsdb

Mas provavelmente a aplicação de teste está rodando localmente e não consegue acessar esse host (database-api-cursos). Esse nome parece ser de um container Docker, e fora do Docker ele não é reconhecido.

Soluções possíveis:

Ignorar os testes

Se você só quer empacotar o projeto, rode:

mvn clean package -DskipTests

Isso pula os testes, e o build deve funcionar.

Rodar os testes com um banco de dados disponível

Se quiser rodar os testes, você precisa garantir que:

  • O PostgreSQL está rodando.
  • Está acessível no endereço database-api-cursos (dentro do Docker Compose, por exemplo).
  • Ou mudar a URL para localhost, caso rode o banco localmente.

Exemplo de application-test.properties para testes locais:

spring.datasource.url=jdbc:postgresql://localhost:5432/logsdb
spring.datasource.username=alura
spring.datasource.password=nJ6vsW37rCd1v88

E configure o application.properties para usar esse perfil de teste automaticamente nos testes:

spring.profiles.active=test

Confira o erro exato no arquivo:

logs/target/surefire-reports/LogsApplicationTests.txt

Lá vai ter a mensagem completa do erro (tipo: Connection refused, host not found, etc.).