I'm trying to use two data sources for my Micronaut web application written in Kotlin: the production one with a mysql database, and a test one to be used with the in memory H2 database.
I'm sure that the test environment is used when executing the test due to this log entry:
INFO i.m.context.env.DefaultEnvironment - Established active
environments: [test]
and I've tried using only the h2 database as the only data source, so the configuration fields regarding it should be correct.
The application runs as expected using the production environment connecting to the local mysql instance by executing ./gradlew run
- I've tried placing the data sources in separate configuration files,
application.ymlandapplication-test.ymlinsrc/main/resources/directory and also placing onlyapplication-test.ymlinsrc/test/resources/, without result. - Adding
@MicronautTestannotation declaring there thetestenvironment andapplication = BookieSparaerverApplicationparams didn't work. - Adding
@TypeHint(Genre::class, Book::class, DefaultGenreRepository::class)annotation toBookieServerApplicationdidn't work.
These are the relevant fields of the application.yml configuration file
datasources:
production:
url: 'jdbc:mysql://localhost:3306/bookie-server?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false'
dbCreate: create-update
driverClassName: com.mysql.cj.jdbc.Driver
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
username: root
password: root
test:
url: ${JDBC_URL:`jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE`}
username: ${JDBC_USER:sa}
password: ${JDBC_PASSWORD:""}
driverClassName: ${JDBC_DRIVER:org.h2.Driver}
jpa:
production:
entity-scan:
packages:
- 'com.transgressoft.bookieserver.domain'
properties:
hibernate:
hbm2ddl:
auto: update
show_sql: true
test:
entity-scan:
packages:
- 'com.transgressoft.bookieserver.domain'
properties:
hibernate:
hbm2ddl:
auto: update
show_sql: true
When I execute the test (GenreControllerSpec in the code), looks like the EntityManager bean is not created and therefore the DAO class cannot be instantiated (in my code is the GenreRepository class).
This is the full log message:
Internal Server Error: Failed to inject value for parameter [entityManager] of class: com.transgressoft.bookieserver.domain.$DefaultGenreRepositoryDefinition$Intercepted
Message: No bean of type [javax.persistence.EntityManager] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
Path Taken: new $GenreControllerDefinition$Intercepted([GenreRepository genreRepository],BeanContext beanContext,Interceptor[] interceptors) --> new $DefaultGenreRepositoryDefinition$Intercepted([EntityManager entityManager],ConfigurationProperties applicationConfiguration,BeanContext beanContext,Interceptor[] interceptors)
io.micronaut.http.client.exceptions.HttpClientResponseException: Internal Server Error: Failed to inject value for parameter [entityManager] of class: com.transgressoft.bookieserver.domain.$DefaultGenreRepositoryDefinition$Intercepted
After setting io.micronaut.context.condition package log level to TRACE, this logs makes me think that the application is unable to find @Entity annotatted classes in the package defined in the application.yml configuration file:
DEBUG i.m.context.condition.Condition - Bean [Definition: io.micronaut.jdbc.spring.DataSourceTransactionManagerFactory] will not be loaded due to failing conditions:
DEBUG i.m.context.condition.Condition - * Custom condition [class io.micronaut.jdbc.spring.HibernatePresenceCondition] failed evaluation
...
DEBUG i.m.context.condition.Condition - Bean [Definition: io.micronaut.configuration.hibernate.jpa.EntityManagerFactoryBean] will not be loaded due to failing conditions:
DEBUG i.m.context.condition.Condition - * Custom condition [class io.micronaut.configuration.hibernate.jpa.condition.EntitiesInPackageCondition] failed evaluation
DEBUG i.m.context.condition.Condition - Bean [Definition: io.micronaut.configuration.hibernate.jpa.EntityManagerFactoryBean] will not be loaded due to failing conditions:
DEBUG i.m.context.condition.Condition - * No bean of type [interface org.hibernate.boot.SessionFactoryBuilder] present within context
DEBUG i.m.context.condition.Condition - Bean [Definition: io.micronaut.configuration.hibernate.jpa.EntityManagerFactoryBean] will not be loaded due to failing conditions:
DEBUG i.m.context.condition.Condition - * No bean of type [interface org.hibernate.boot.SessionFactoryBuilder] present within context
ERROR i.m.h.s.netty.RoutingInBoundHandler - Unexpected error occurred: Failed to inject value for parameter [entityManager] of class: com.transgressoft.bookieserver.domain.$DefaultGenreRepositoryDefinition$Intercepted
The full project code is accesible here https://github.com/octaviospain/bookie- reposerver/tree/feature/jpa-and-hibernate and to reproduce above logs just execute ./gradlew test and open the report, or run it with logs activated.
Aucun commentaire:
Enregistrer un commentaire