Home > Enterprise >  How can I override the JPA properties for multiple datasources for Integration tests?
How can I override the JPA properties for multiple datasources for Integration tests?

Time:05-28

I have sucessfully configured two datasources for two different databases and schemas in my Spring Boot application. Now, for the integration tests I want to use an embedded database (HSQL) and execute the tests there. I tried overriding the properties using the following file (/src/test/resources/application-test.properties)

eot.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
eot.datasource.username=sa
eot.datasource.password=sa
eot.datasource.url=jdbc:hsqldb:mem:test;DB_CLOSE_DELAY=-1
eot.datasource.hikari.pool-name=ptest-eot
eot.datasource.jpa.show-sql=true
eot.datasource.jpa.generate-ddl=true
eot.datasource.jpa.database-platform=org.hibernate.dialect.HSQLDialect
eot.datasource.jpa.properties.hibernate.ddl-auto=create
eot.datasource.jpa.properties.hibernate.dialect=org.hibernate.dialect.HSQLDialect       

info.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
info.datasource.username=sa
info.datasource.password=sa
info.datasource.url=jdbc:hsqldb:mem:test;DB_CLOSE_DELAY=-1
info.datasource.hikari.pool-name=ptest-info
info.datasource.jpa.show-sql=true
info.datasource.jpa.generate-ddl=true
info.datasource.jpa.database-platform=org.hibernate.dialect.HSQLDialect
info.datasource.jpa.properties.hibernate.ddl-auto=create
info.datasource.jpa.properties.hibernate.dialect=org.hibernate.dialect.HSQLDialect

In the log I can see it picks up some of the properties. In my application.yml the pool's name is 'eot-pool' and when I run the tests it shows correctly as 'ptest-eot'.

2022-05-27 17:09:03.855  INFO 6592 --- [    Test worker] mx.com.gnp.crm.adfe.EotJpaConfiguration  : org.springframework.boot.autoconfigure.jdbc.DataSourceProperties@18a1fd92
2022-05-27 17:09:04.039  INFO 6592 --- [    Test worker] com.zaxxer.hikari.HikariDataSource       : ptest-eot - Starting...
2022-05-27 17:09:04.845  INFO 6592 --- [    Test worker] com.zaxxer.hikari.pool.PoolBase          : ptest-eot - Driver does not support get/set network timeout for connections. (característica no soportada)
2022-05-27 17:09:04.851  INFO 6592 --- [    Test worker] com.zaxxer.hikari.HikariDataSource       : ptest-eot - Start completed.

But it's not overriding the JPA properties. It's not creating the schemas, tables, nor printing the SQL statements to the log.

I tried removing the 'properties' part. Using for example:

info.datasource.jpa.show-sql=true
info.datasource.jpa.hibernate.ddl-auto=create
info.datasource.jpa.hibernate.dialect=org.hibernate.dialect.HSQLDialect

But the JPA properties aren't being replaced.

When I run the tests, the log shows the wrong dialect (from the main application.yml file).

HHH000400: Using dialect: org.hibernate.dialect.DB2400Dialect

And when the tests runs:

SQL Error: -5501, SQLState: 42501 

Because the schema and the table don't exist.

How can I override the JPA properties for integration tests when I have multiple datasources?

CodePudding user response:

It seems you have a general application.yml file and a specific application-test.properties file for the test environment. I'm not sure if you can mix those file extensions, probably not. Either you choose to use .yml or .properties for both. Try to change the file name to application-test.yml. Also, to activate this specific test environment it's necessary to put this config in application.yml:

spring:
   profiles:
       active:test
  • Related