I have been comfortable using JUnit in core java application.
When I had to deploy a WAR in a Tomcat server, I could not get a handle on Unittesting my DAOs or the Domain model. I don't have to test the web components, but wish to test the Domain model that the Spring Controllers would use.
I am using Eclipse IDE.
Could you please help me on any tool or plugin that would help me unit test cases reach the code.
I don't see why it should be different to use JUnit for unit testing a Spring based web application. In the end at least most of your classes should be ordinary POJOs if you're using any modern web framework. So there's nothing really different compared to ordinary Java applications. If you have to deal with components of the Servlet API for example it's easy enough to create an ordinary stub class for testing purposes because most of these components are simple interfaces.
If you want to do in-container integration testing it's of course something different. Then another testing framework like Cactus or JUnit add-ons could be helpful, but you most probably don't need them for simple unit tests if your application is reasonably designed.
Joined: Oct 08, 2002
Thanks for your guidance.
True that I would want to test my JPA POJOs and the Domain Model. I would write test cases for the methods of DAOs. It requires EntityManager. When I write the code, I expected the Entity Manager to be Dependency injected by the J2EE-JPA Framework.
In such a situation, when I execute unit test cases for the DAOs, it throws null pointer exception fot the Entity Manager. In the real life situation, the EntityManager is an administered object.
I wish to use JUnit. Could you please guide me on what I am missing.
now I understand what's your problem. Well, there are different ways to achieve your goal and make your entities testable.
First it would be a good idea to externalize your entities dependency on an EntityManager, i.e. make the EntityManager "injectable" via a constructor or setter (if it not already is). Then it would be easy to pass in different EntityManager during tests.
For easier test cases it could be sufficient to simply create an EntityManager stub and inject it into your class under test which could then just have fake implementations for the EntityManager methods you need.
An alternative way would be to set up a "real" EntityManager but to use an in-memory database like Apache Derby or HSQLDB with real data during tests.
Helpful frameworks which could be handy for database/JPA based tests could be DBUnit and Ejb3unit. Just google for it