Я пишу тесты для моего приложения Spring Boot. Обычно он использует Postgres в качестве источника данных, но в тестах я хочу отдельный test-dabase. Я обнаружил, что @AutoConfigureTestDatabase
может использоваться для автоматической замены моего источника данных любой базой данных в памяти, найденной на пути к классам.
Поэтому я добавил HSQLDB к своему pom.xml
, и мои тесты не запустились с ошибкой:
2020-01-22 18:48:11.137 INFO 29591 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.3.11.Final}
2020-01-22 18:48:11.139 INFO 29591 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2020-01-22 18:48:11.284 INFO 29591 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2020-01-22 18:48:11.469 INFO 29591 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
2020-01-22 18:48:11.508 INFO 29591 --- [ main] o.h.e.j.e.i.LobCreatorBuilderImpl : HHH000421: Disabling contextual LOB creation as hibernate.jdbc.lob.non_contextual_creation is true
2020-01-22 18:48:11.513 INFO 29591 --- [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@611c3eae
2020-01-22 18:48:12.700 WARN 29591 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: -5501, SQLState: 42501
2020-01-22 18:48:12.700 ERROR 29591 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : user lacks privilege or object not found: PG_CLASS
2020-01-22 18:48:12.705 ERROR 29591 --- [ main] o.s.b.web.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'securityConfiguration.AuthenticationConfiguration': Unsatisfied dependency expressed through field 'phonePasswordFilter'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'phoneNumberPasswordAuthenticationProcessingFilter' defined in file [/Users/artemvrby/Projects/Java Apps/Air Cloud Solutions/Central API/Central API/target/classes/org/aircloud/central_api/security/auth/phone_number_password/PhoneNumberPasswordAuthenticationProcessingFilter.class]: Unsatisfied dependency expressed through constructor parameter 3; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through method 'configureAuthenticationManager' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'phoneNumberPasswordAuthenticationProvider' defined in file [/Users/artemvrby/Projects/Java Apps/Air Cloud Solutions/Central API/Central API/target/classes/org/aircloud/central_api/security/auth/phone_number_password/PhoneNumberPasswordAuthenticationProvider.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jpaUserSecurityDetailsService' defined in file [/Users/artemvrby/Projects/Java Apps/Air Cloud Solutions/Central API/Central API/target/classes/org/aircloud/central_api/security/JpaUserSecurityDetailsService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userSecurityRepo': Cannot create inner bean '(inner bean)#70d77826' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#70d77826': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is 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.SQLGrammarException: Unable to build DatabaseInformation
2020-01-22 18:48:12.734 WARN 29591 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
2020-01-22 18:48:12.742 INFO 29591 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-01-22 18:48:12.755 ERROR 29591 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
...
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: PG_CLASS
at org.hsqldb.error.Error.error(Unknown Source) ~[hsqldb-2.3.4.jar:2.3.4]
at org.hsqldb.error.Error.error(Unknown Source) ~[hsqldb-2.3.4.jar:2.3.4]
...
Я обнаружил, что это связано со свойством spring.jpa.database=POSTGRESQL
. Комментирование этого вопроса устраняет проблему, но вызывает вопросы:
1) Что на самом деле означает spring.jpa.database=POSTGRESQL
и как это помогает? Описание говорит: " Целевая база данных для работы, автоматически определяется по умолчанию. Может быть альтернативно установлена с помощью свойства" databasePlatform ". ".
2) Я считаю, что должен быть способ исправить это, особенно если рекомендуется указать spring.jpa.database=POSTGRESQL
.