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.
Related
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.
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
I have an application pointing to a mysql database.
I have been trying to use DBUnit to set up my tests environments, which works fine.
The problem is that when configuring DBUnit I pointed it to the SAME mysql database. So when DBUnit is executed, it takes the specified dataset.xml and overrides the information from my original database. which makes sense because there is where I am pointing it to.
The question is, am I supposed to create a new database only for tests so my DBUnit can point to it? If so, how would I manage the structure synchronization between my original database and the one for tests?
Thanks in advance.
am I supposed to create a new database only for tests so my DBUnit can point to it?
It is a better approach to do so as it eliminates multiple problems.
how would I manage the structure synchronization between my original database and the one for tests?
You don't mention tech in your persistence stack, such as Hibernate, Spring/Spring Boot/Spring Data/Flyway/LiquiBase/etc. to suggest more of how to implement this. In general, run DDL in the schema at tests run startup (either from managed DDL from something like Flyway or auto-generation from Hibernate).
Additionally, my preferred and typical testing approach is with:
An in-memory/embedded database for its speed, such as Apache Derby, automatically started just before launching tests.
Create tables in schema using Hibernate DDL gen from annotated entities.
No existing rows in any tables; happens automatically with an embedded database and a clean build when storing any of its files in a subdirectory of the build output dir.
dbUnit configured with DatabaseOperation.CLEAN_INSERT [0].
Minimal dbUnit data for each test.
I have problem with unit tests for persistance stuff written in spring data jpa.
For particular repositories I have a unit tests to be sure that everything works correctly. Also I have a integration tests. Each tests are passed when I run it for particular test classes. But when i run a whole package of tests I got a lot of faliures because I have records inserted into DB from previous tests.
Of course in each test classes I can add #After method to clean each data but I would like to ask that it posible to clean all data from DB before run tests from particular test classes without adding #After method?
Best Regards.
Use Spring's transactional test support to ensure that database changes are rolled back after each test:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html#testcontext-tx
One common issue in tests that access a real database is their effect
on the state of the persistence store. Even when you’re using a
development database, changes to the state may affect future tests.
Also, many operations — such as inserting or modifying persistent
data — cannot be performed (or verified) outside a transaction.
The TestContext framework addresses this issue. By default, the
framework will create and roll back a transaction for each test.
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?