T
T
takret2014-12-24 16:09:32
Java
takret, 2014-12-24 16:09:32

How to unit test a DAO object in Java (JUnit)?

Good afternoon.
How do you typically implement DAO unit testing? Are you testing each method independently, or are you testing all methods in one test method? The fact is that I tried to implement the test using the TestNG library, since it is possible to determine the dependencies of tests on each other, but at work they scolded me for this and told me to only use JUnit, so this is no longer unit testing.
But I can't figure out how to conduct such independent testing for a DAO object if there is a connection between some methods.
I'll give you an example. For example, two methods:
1) private Entity getEntityById(long id)
2) private void deleteById(long id)
After all, it is logical here that in order to properly test delete, I need to use the getById method to check that the entity was successfully deleted.
Isn't test method dependency always a bad thing?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolai, 2014-12-24
@j_wayne

Unit testing is usually done "one test per method". The border here is rather blurred, but IMHO it makes sense. I can’t say exactly how this is done exactly in Java, but in ruby, in this case, I request the entity not through the business layer method, but directly from the active record. In your case, this would be EntityManager (or equivalent from Spring Data).
For me, independent tests are definitely better than dependent ones, for example, when it crashes, it’s easier to understand. what exactly fell off.
In general, there is an approach when the interaction with the database is stabilized / mocked completely, which allows you to write supersonic tests. Although I personally think that in-memory db is enough.

N
Nikolai Pavlov, 2014-12-25
@gurinderu

DAO layer, in which there is no business logic, what are they going to test there?
Requests? Then this is the usual integration testing, raise h2 and check requests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question