Im just trying to setup an unit testing environment in the project im working atm.
I just evaluated the different approaches of mock objects and the idea behind JUnit and the in-container approach of Cactus and i still have some questions about it...
If i want to test my DAO classes which retrieve information from a persistent environment (in this case DB) i use a datasource that i lookup through JNDI. JNDI however is a naming service provided by the container. One thing i want to avoid is to create mock objects for all the services, as this would result in lots of mock objects to maintain.
The in-container approach seems to be more logic to me. What do you guys think?
And how can i just test my DAO classes with the JNDI lookup with Cactus?
First of all, note that you are talking about integration tests rather than unit tests. A unit test does not access the database.
Before you complicate things with Cactus, see if pure JUnit will work for you. Can you pass the connection object directly to the DAO? If so, you don't need JNDI for the test which means you can write a regular JUnit test without any other tools.
There are unit tests and then there are integration tests. Unit tests are isolated test of just the specific class (or limited set of classes) that you are working on, just a DAO class or just a service class, etc. Integration testing is when you put the classes in a more real-world test harness like cactus and other in-container testing.
For mocking in unit tests I use JMock (http://jmock.org) which makes the creation of mock objects (with assertions and constraints ) trivial and you dont have to create a lot of extra mock object classes.
DAOs can often be an edge case that are difficult to unit test in a really meaningful manner, but you can still make sure that all the external pieces used are routing things properly.
Good luck and I hope this helps at least a little.
- Chris Stehno, SCPJ
subject: Conceptual questions concerning Unit tests