Spring Boot Rest application: Testing JDBC DAO layer - spring

How can I test my DAO layer in Spring Boot Application if my application only selects information from database and doesn't write anything?
Even more, my application selects data from view.
The common approach is to write some testing data by method with annotation #BeforeEach and to delete them by method with annotation #AfterEach.
But because my application performs query to view, I can't insert any data in database.
Is there any opportunity to test my DAO layer?

You have a few options:
Use an embedded H2 database then seed it with a data.sql, which you can dump from your test database.
Use DBUnit and define your data in an xml file.
For you I think data.sql is the way to go. Just add a data.sql to your test/resources file and it will be picked up by JPA.

Related

How to create a stored procedure, with Spring JPA but without using Entities

In a project that I am working with Spring and JPA, I need to call a stored procedure but it does not have any table stored on the application side, only an input parameter is sent and returns an output parameter to know if I finish the process.
In all examples with JPA it always relates to JPA entities and the BD Tables.
Thank you for your contributions.
Take JPA out the pricture then. Assuming your application has a Datasource configured then inject that to the relevant code and get a connection:
https://docs.oracle.com/javase/7/docs/api/javax/sql/DataSource.html#getConnection()
Then just use standard JDBC.
https://docs.oracle.com/javase/tutorial/jdbc/basics/storedprocedures.html

How can you use h2 in memory data in a Spring Boot API?

We are building a new API using Spring-Boot. For testing, we want to use the h2 in memory data base to test our API.
How do you setup h2 db so Spring-boot can use that data during that session?
Ideal situation, I can call GET and retrieve everything that was created in my h2 db
In your test folder, create a Defaultdatasourceconfig class, that will have all the necessary beans (like DSbean, TXManager bean etc). For the new DriverManagerDataSource() method, pass the necessary datasource properties (create a properties class with the below fields and annotate them with #Value and the property names), add those properties to your application-test.properties file
db.hsql.url=jdbc:hsqldb:mem:testdb
db.hsql.username=sa
db.hsql.password=sa
db.hsql.driver.name=org.hsqldb.jdbc.JDBCDriver
spring.datasource.platform=hsqldb
If you want to load specific tables related only for your usecase, add this to the prperties files spring.datasource.schema=classpath:abc.sql and add abc.sql under the same folder as application-test.prpoerties.
So, when you run the test, when the context builds, it will use hsql db as Primary db, loads table by executing the sql and uses this db.
you will need to add hsql driver in your classpath.
Same can be done with h2

Pass Spring datasource to Javers to Audit DTOs

In My Spring Boot application, I would like to audit DTOs instead of entities and move audit logic to common library which will have #EnableAudit annotation to audit DTO at method level. If I am using JaversBuilder.javers().build(); in common library I would not be able to commit data as it would not know anything of datasource.
I want to create Javers instance in my application and pass datasource to it and then pass Javers instances to common library to perform commit. Is there anyway in Spring boot application to create Javers instance and associat datasource to it which will be used at the time of commit ?
If you take javers-spring-boot-starter, you will get a Javers instance created as a Spring bean. It will be connected to your database and ready-to-use. See https://javers.org/documentation/spring-boot-integration/

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?

Integration testing of EJBs

I have a set of EJBs, which persist and retrieve data from a Sybase database through a set of DAO classes.
The DAO classes are POJOs, which execute directly sql procedures(prepared statement) over JDBC datasource.
What would be the appropriate framework to prepare an integration testing suite for these EJBs ,using some meaningful simulated data in the database?
Does spring/junit provide some help?
Any advice is appreciated.
I think unit testing is enough depending on what you're trying to validate.
If you need to test only the EJB I would go with a mocked version of your DAO that work in-memory (a DAO with a HashMap to save and retrieve data for example) so you can focus your tests on the input and output of your EJB methods. This way you can also populate your test data programmatically.
If you also need to test the Sybase statements generated by your DAO I think you could separate the methods that actually execute the statements from those that generate the SQL, this way you could validate the SQL generated without needing a connection to the database.
If you really need to validate your tests against the database engine you could try to configure a Profile for your Integration tests with a valid database connection and bind this connection to your DAO bean. You can even populate this database with some testing data via script.

Resources