How to make a Spring Boot project dependency use H2 database - spring-boot

I have a Spring Boot project (MAIN) which in turn depends on a (DAL) dependency.
DAL is where every Entity, Repository, Projection and Spring Data JPA related configurations are.
Now, MAIN project is a scheduler, and I need to make some integration tests over it. As always I want to use H2 database for the job in hands.
I have the H2 configuration under /test/resources/application.properties and this configuration have always worked if the JPA related classes were in the same project.
But in this specific case what is happening is that the H2 configuration are being ignored and the Integration Tests are writing into the real database.
Is possible to make the DAL to use H2 configurations?

For future reference, seting up H2 configuration for nested projects is very much possible.
The reason I was unable te setup this configuration rigth on the first attempt was because on DAL I have configured multiple data sources, as such the default h2 configuration wouldnt be enought.
So I had to make something like the folloing:
first-datasource.driver-class-name=org.h2.Driver
first-datasource.jdbcUrl=jdbc:h2:mem:testdb
first-datasource.username=sa
first-datasource.password=password
second-datasource.driver-class-name=org.h2.Driver
second-datasource.jdbcUrl=jdbc:h2:mem:testdb
second-datasource.username=sa
second-datasource.password=password

Related

How can use an in-memory H2 database when testing my Quarkus application?

I plan to use PostgreSQL as the database for my Quarkus application but I would like the convenience of using H2 in my tests.
Is there a way I can accomplish such a feat?
Quarkus provides the H2DatabaseTestResource which starts an in memory H2 database as part of the test process.
You will need to add io.quarkus:quarkus-test-h2 as a test scoped dependency and annotate your test with #QuarkusTestResource(H2DatabaseTestResource.class).
You will also need to have something like:
quarkus.datasource.url=jdbc:h2:tcp://localhost/mem:test
quarkus.datasource.driver=org.h2.Driver
in src/test/resources/application.properties
In order for the application use PostgreSQL as part of its regular run, quarkus-jdbc-postgresql should be a dependency and
quarkus.datasource.url=jdbc:postgresql://mypostgres:5432
quarkus.datasource.driver=org.postgresql.Driver
should be set in src/main/resources/application.properties
Update
As of version 1.13, Quarkus can launch H2 automatically in dev and test mode when quarkus-jdbc-h2 is on the classpath and no URL configuration is provided.
See this for more information.
You can use the below configurations in the application.properties file to use h2 database
quarkus.datasource.jdbc.url=jdbc:h2:mem:default
quarkus.datasource.driver=org.h2.Driver
quarkus.datasource.username=admin
quarkus.hibernate-orm.database.generation=drop-and-create

How to disable Javers for integration tests?

I am using Javers 3.11.2 with Spring Boot 1.5.17. When I am trying to run integration tests on an embedded database I still see that Javers tables are getting created each time.
Is there a way I can disable Javers during these tests so that these tables will not be created each time?
There is the easy way, put:
javers:
sqlSchemaManagementEnabled: false
in your application-test.yml. See https://javers.org/documentation/spring-boot-integration/
Disclaimer: I've never used Javers.
In general, disabling something in "integration tests" means that you don't want to load some beans (of Javers in this case).
This means in turn that you have to exclude them from the list of configurations spring boot works with.
If you're using javers autoconfiguration module, it has to provide in its own "spring.factories" file (can be found inside the jar) a file for autoconfiguration.
Find its java code and see whether it has some "#Conditional on something (property beans, etc.)" If it has than create a profile for integration test that will configure the beans in a way that conditional in javers won't pass and the bean won't be created as a consequence
If it doesn't have a conditional on something like this, you'll have to exclude the whole configuration. Its usually can be done by annotation #SpringBootApplication(exclude=<JaversAutoconfiguration goes here>
This will, however, turn it off also for production usage, which is obviously not something that you want. So for "production" profile, you'll have to import it as a regular configuration (not an autoconfiguration), for integration test profile you won't need this.

Using Liquibase and Spring Boot

I have a Spring Boot application and want to use Liquibase to generate the changelogs for my JPA entities. However, I encounter different issues, depending on my approach.
My first approach is to use the diff goal of the maven plugin. The url is my H2 development database with the H2 driver and the reference URL is something like "hibernate:spring:myBasePackage.myEntityPackage?dialect=org.hibernate.dialect.H2Dialect" with the driver "liquibase.ext.hibernate.database.connection.HibernateDriver". In that case Liquibase seems to recognize my entities, but prints the differences to the console. Also the differences do not have the form of a changelog file.
My second approach would be to use the generateChangeLog goal of the maven plugin. In this case my url is "hibernate:spring:myBasePackage.myEntityPackage?dialect=org.hibernate.dialect.H2Dialect" with the driver "liquibase.ext.hibernate.database.connection.HibernateDriver. In this case I am getting an error "Unable to resolve persistence unit root URL: class path resource [] cannot be resolved to URL because it does not exist". This error can be found in both the Spring an the Liquibase issue trackers, but it seems like it is always said that this error is already fixed.
My third approach is basically like the second, but in this case I am using a "hibernate:classic" url with an implementation of "CustomClassicConfigurationFactory", which registers my annotated classes explicitly. This does work. However, in this case I have to do this in my application-jar. I have to add my application-jar as a dependency for the maven-plugin. Thus I have to build my application-jar (and install it to the local Maven repository), before I can generate the changelogs. This seems to be cumbersome.
My questions are:
Is there an easier way to generate the changelogs for JPA entities in a Spring boot based application?
Why are the first two approaches not working?
Is there a way to simplify the third approach?
I am using:
Spring Boot 1.5.4.RELEASE
Liquibase-Hibernate4 3.6
Liquibase 3.5.3
Many thanks in advance.
In the first approach using liquibase:diff , the change set for the entities (create table change set) will not be generated since liquibase do not assume new jpa entity as change.
In the second approach generateChangeLog, it generates the change log from the given data base.It won't look into your jpa entities.
In order to generate the ddl scripts for your jpa entities , just submit the following to your jpa properties
<property key="javax.persistence.schema-generation.scripts.action">drop-and-create</property>
<property key="javax.persistence.schema-generation.scripts.create-target">./ddl/create.sql</property>
<property key="javax.persistence.schema-generation.scripts.drop-target">./ddl/drop.sql</property>
The above will generate the scripts the ddl folder under the root folder.
You can check the other properties here https://thoughts-on-java.org/standardized-schema-generation-data-loading-jpa-2-1/

Spring Boot Tests within a Container

I have coded a Spring Boot based web application, which is expected to be run in WildFly server. The applications runs great, but the issue is with testing.
I have the database connections, caching and transaction management dealt by the server. Now, I need to be able to test them. While I was able to get through database connection problem through a mock JNDI connection and the transaction management, I'm not sure how to deal with testing of the caching.
One solution is to use Arquillian project. But, either this project is unable to recognize Spring Boot/ I'm doing something wrong, which is causing me pain to test the application.
Can someone please suggest on solving the issue? Below are my hibernate specific properties
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.properties.hibernate.cache.region.factory_class=org.jboss.as.jpa.hibernate4.infinispan.InfinispanRegionFactory
spring.jpa.properties.hibernate.cache.infinispan.cachemanager=java:jboss/infinispan/container/hibernate
spring.jpa.properties.hibernate.transaction.manager_lookup_class=org.hibernate.transaction.JBossTransactionManagerLookup
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.cache.use_query_cache=false
spring.jpa.properties.hibernate.hbm2ddl.auto=none
spring.jpa.properties.hibernate.generate_statistics=true
spring.jpa.properties.hibernate.cache.infinispan.statistics=true
spring.jpa.properties.hibernate.search.default.directory_provider=infinispan
spring.jpa.properties.hibernate.search.infinispan.cachemanager_jndiname=java:jboss/infinispan/container/hibernate
I would suggest creating a separate configuration for tests. This configuration would contain a definition of a TransactionManager bean - here is an example from other post. The next step is to provide your own implementation of TransactionManagerLookup and applying it to Transport configuration - as described in the manual.

Spring Boot app testing with database vendor specific JPA annotation

I got an app with an existing database that has Oracle only field type (e.g. "binary-float") mapped on the entity class with #ColumnDefinition annotation.
Things runs fine when running the app normally by launching the Application class.
However I can't seem to find a way to write junit tests easily. In another Spring Boot app, I have been using different profile to define a normal datasource that points to Oracle and a junit test datasource that points to the h2 in-memory db. I stayed mostly within JPAQL and common sql standard when using direct sql. Problem is, this scheme doesn't work if the JPA mapping annotation is database specific.
Any suggestions?

Resources