valentinacupac / banking-kata-java

Banking Kata (Java)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Docker compose - Databases not automatically created (Workaround)

valentinacupac opened this issue · comments

This is a local issue that appeared on some local machines, pending to find full solution.

When you run:

./gradlew integrationTest

You see the following error:

> Task :integrationTest

BankingApplicationTests > contextLoads() FAILED
    java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
        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.GenericJDBCException at StandardSQLExceptionConverter.java:42
                    Caused by: org.postgresql.util.PSQLException at QueryExecutorImpl.java:2675

JpaBankAccountStorageTest > should_return_empty_given_non_existent_account_number() FAILED
    org.junit.jupiter.api.extension.ParameterResolutionException at ExecutableInvoker.java:239
        Caused by: java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
            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.GenericJDBCException at StandardSQLExceptionConverter.java:42
                        Caused by: org.postgresql.util.PSQLException at QueryExecutorImpl.java:2675

JpaBankAccountStorageTest > should_return_added_bank_account() FAILED
    org.junit.jupiter.api.extension.ParameterResolutionException at ExecutableInvoker.java:239
        Caused by: java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
            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.GenericJDBCException at StandardSQLExceptionConverter.java:42
                        Caused by: org.postgresql.util.PSQLException at QueryExecutorImpl.java:2675

JpaBankAccountStorageTest > should_find_multiple_added_bank_accounts() FAILED
    org.junit.jupiter.api.extension.ParameterResolutionException at ExecutableInvoker.java:239
        Caused by: java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
            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.GenericJDBCException at StandardSQLExceptionConverter.java:42
                        Caused by: org.postgresql.util.PSQLException at QueryExecutorImpl.java:2675

BankAccountControllerSystemTest > should_open_account_given_valid_request() FAILED
    org.junit.jupiter.api.extension.ParameterResolutionException at ExecutableInvoker.java:239
        Caused by: java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
            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.GenericJDBCException at StandardSQLExceptionConverter.java:42
                        Caused by: org.postgresql.util.PSQLException at QueryExecutorImpl.java:2675

22 tests completed, 5 failed

> Task :integrationTest FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':integrationTest'.
> There were failing tests. See the report at: file:///C:/Users/valen/GitHub/valentinacupac/banking-kata-java/build/reports/tests/integrationTest/index.html

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 23s
8 actionable tasks: 8 executed

You open up the detailed file (e.g. file:///C:/Users/valen/GitHub/valentinacupac/banking-kata-java/build/reports/tests/integrationTest/index.html above).
You open up the error log for BankingApplicationTests. contextLoads()

As you scroll to the bottom, you see the underlying cause is FATAL: database "banking_kata_db" does not exist:

java.lang.IllegalStateException: Failed to load ApplicationContext
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
	at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
	at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
    ...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
	at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804)
	at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
	at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
    ...
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
	at app//org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421)
	at app//org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
	at app//org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
    ...
Caused by: org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
	at app//org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
	at app//org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
	at app//org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
    ...
Caused by: org.postgresql.util.PSQLException: FATAL: database "banking_kata_db" does not exist
	at app//org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2675)
	at app//org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2787)
	at app//org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:173)
    ...

Root Cause

The root cause is that for some reason, on some local machines, the docker-entrypoint-initdb.d seems not to be executed in docker.compose.yaml
This issue was only noted on some local machines, but on GitHub Actions everything works well.

Workaround:

Open up Docker Desktop, go into the Docker container postgres and open up its CLI.

Then input the following:

psql -U postgres;
\l
CREATE DATABASE banking_kata_db OWNER postgres;
CREATE DATABASE keycloak_db OWNER postgres;
\l

Then re-run the integration tests and verify they pass:

./gradlew integrationTest

If you experienced the issue we described in this ticket, please comment below.
If you tried the workaround, please let us know whether or not it worked for you.
If you tried and found any other solutions, please comment too.

If you experienced the issue we described in this ticket, please comment below.
If you tried the workaround, please let us know whether or not it worked for you.
If you tried and found any other solutions, please comment too.

Currently we're not sure if this is a one-off issue or if others are experiencing it.