How to use JUnit and Hibernate?

I want to use JUnit to validate Hibernate code, such as the insert, update, delete, .. and transaction management method.

But I do not know how useful it is to use w100 for Hibernate and what I should check with Hibernate.

How to check DAO methods?

Hope you could give me some guides!

+6
unit-testing junit orm hibernate
source share
6 answers

You can use DBUnit to test the DAO layer. Because you need data for testing.
Example: DBUnit xml inserts dummy data into the database that you describe, and then you can call assertEquals ("myname", userDAO.findById (1) .getName ()); etc. After the test, you can delete the dummy data using DBUnit . Check in detail .

Documentation
Sleep mode with dbunit
DBUnit and Hibernate

+4
source share

Depending on your scenario, you can use several approaches

  • use the built-in database (HSQLDB) for your unit tests. Paste all the requested data into @Before and delete in @After . This, however, is not quite a β€œsingle” test, since it depends on some external preconditions.
  • You can make fun of your dao (e.g. Mokcito) so that it does not interfere with the database. This can be useful when testing your level of service, and you don't care what is stored in the database.
+2
source share

I use the Chris Richardson approach described in POJO in the action book

In-memory SQL database

Arguments

  • No network traffic
  • No access to disk.
  • Useful to test queries

against

  • Is its schema similar to a production database schema?

Named Queries

Arguments

  • It can be stored separately from the repository, which allows you to test it without repositories.

against

  • Doesn't work fine when using dynamic queries

Mock repositories

Arguments

  • Reduces database access
  • Reduces the number of test cases

against

  • It is required to check database queries separately

DBUnit

Arguments

  • This is a jUnit extension

against

  • You need to load an XML file containing the expected values
  • Error if you missed a new display property

Yours faithfully,

+2
source share

OK, a few points.
First of all, if you have to test the actual code that speaks to DB , use DBUnit to make your life easier, and it is recommended to use HSQLDB so that your tests can tune their environments at runtime, without having to install and configure the database.

Secondly, if you should not talk to the database , I would use a shared fake library (e.g. EasyMock, JMock or Mockito), and do the tests do not really talk to the database, which usually makes the tests faster and easier.

0
source share

Personally, I am very careful about using embedded databases such as HSQLDB with Hibernate, and expect everything to work the same way when you move it to an Oracle / MySQL / SQL server. Sleep mode is too impenetrable for abstraction.

I have no experience with systems other than JUnit. I find this works well. Here are some things that I always remember:

  • Unit tests for CRUD database operations should never assume the presence or absence of specific data. Everything that is inserted must also be deleted or rolled back.
  • Be sure to clear the base connection object. This will actually perform caching operations and sets triggers in the data model.
0
source share

If you created a DAO object, you can then send it some objects, call save, and then get these objects and check the field values. This is a very basic hibernation test.

Make sure that you clear your data though in setup methods or break. If you save objects, delete them.

Rails best practices are to use a separate testing database populated with test data. I would not do this against a production database; if you have a development database for yourself that you can easily update the data, just use this.

0
source share

All Articles