aspose file tools
The moose likes Testing and the fly likes Junit for Wars deployed in Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » Testing
Reply Bookmark "Junit for Wars deployed in Tomcat" Watch "Junit for Wars deployed in Tomcat" New topic
Author

Junit for Wars deployed in Tomcat

Rajesh So
Ranch Hand

Joined: Oct 08, 2002
Posts: 111
Hello,

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 Unit testing 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.

Regards,
Rajesh
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1220

Hi Rajesh,

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.

Marco
Rajesh So
Ranch Hand

Joined: Oct 08, 2002
Posts: 111
Hello Marco,

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.

Regards,
Rajesh
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1220

Hi Rajesh,

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

Marco
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Junit for Wars deployed in Tomcat
 
Similar Threads
unit testing with abbot and junit
Testing Eclipse Plugin
testing JSP applications with JUnit
Procedural programming shortcuts in OOD?
Facade and Delegate