I need to write tests for data access (Hibernate persistence actions) because I want to know that it works as expected even if I upgrade Hibernate version. So what is the best way to do it with Junit?? And is it really useful to test an external system (Hibernate JPA implementation) ??
Asked
Active
Viewed 353 times
2
-
IMHO, testing someone elses software is mostly useless because what will the consequences be if you find an error - change their code? No, I'd rely on their quality of code and concentrate on my own home-grown code. – Smutje Jul 24 '19 at 10:43
-
Are you using Spring? – Jose Martinez Jul 24 '19 at 10:46
-
Test your own code. In any way, you will need to find a way in which hibernate works with your code – XtremeBaumer Jul 24 '19 at 10:46
-
I presume you want to test your way of mapping the entities to your dbms and if that works even if you change the version of Hibernate or JPA you are using. That I find quite reasonable. This can be tested using arquillian in the destination server or ioc-unit-ejb even if you don't use ejb inside a weld-container. Example: https://github.com/1and1/ioc-unit/tree/master/examples2/ex1-1entity – aschoerk Jul 24 '19 at 10:47
-
My concern is that I want to know if the DB layer works as expected even if I upgrade my Hibernate version, so I will be able to detect if there is any problem if I increase the Hibernate version. – khouloud Jul 24 '19 at 10:52
-
Usually, to test a whole application, I introduced system tests (!= unit tests) who worked the system from the outside and used interfaces/command line calls/message queues to identify if the actual "business use cases" of my system are running through - against a fully fledged test environment *with* database and stuff. – Smutje Jul 24 '19 at 11:04
-
In my case I'm using Junit, I switch my database with an embedded derby database, so I will be able to persist my entities in every test case. – khouloud Jul 24 '19 at 11:13
1 Answers
0
Testing someone else's code is not really useful as you can't as mention change it. But in case you want something useful you can try the dbunit ( http://dbunit.sourceforge.net/ ) to setup the db the way you like before the test and then assert the contents of the db.. Also there is a wrapper around dbunit that works with spring ( https://springtestdbunit.github.io/spring-test-dbunit/ ) which make even easier the setup and assert part of the db with annotations.

Michael Michailidis
- 1,002
- 1
- 8
- 21