I am looking for a best practice to test a DAO class with jUnit. My DAO class has a couple of typical DAO methods like createUser(User user), deleteUser(Long id), updateUser(User user), findUserById(Long id)…
So createUser could be easy, I could create a user and check if it has an id afterwards. If yes the test would pass.
Or would you rather create a User, read the User from the DB afterwards and check if it
1) finds the user
2) the instance variables from the returned user are the same like from the user saved before
Now what about the deleteUser function? It takes an ID, but in order to obtain the ID I would first have to create a User. So how to do this? Use the testCreateUser method from the test method or the createUser method from the DAO class?
Same thing with updateUser(User user) where I need a User to be updated first and findUserById(Long id) where I need an Id first.
I think my requirement is pretty common so I am wondering if there is something like a design pattern for testing DAOs with jUnit.
Thanks, Paul
IMO a unit test should be able to run by itself.
So for example a delete test will first create a user then delete it and check that it does not exist anymore (by trying to load it).
Relying on previous tests is not a good way because if the previous test fails for any reason, the next test will also fail.