File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes unit testing applications best practices. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "unit testing applications best practices." Watch "unit testing applications best practices." New topic
Author

unit testing applications best practices.

Rakesh Jhamb
Ranch Hand

Joined: Jun 18, 2003
Posts: 154
Hello,

Let's say we have an application that connects to the DB and insert few records. If I want to Unit Test it using Junit, then we will have to connect to the DB and then insert few records and then verify them ? Is this the correct approach. How does it work, as I want to run my unit test cases multiple times. But if I insert same records every time, there will be a unique constraint violation at DB side. How do we handle it. What is the standard practice.

Another question I have is how to unit test web applications that are deployed on containers like tomcat. Should the application be deployed on tomcat and running while unit testing its classes.


Regards,
Rakesh.


SCJP2, SCWCD
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Make sure you're testing what you actually want to be testing--here it sounds like you're testing the database connector, which has probably already been tested.

If you're testing your service methods, it may be enough to make sure they're getting called with the appropriate parameters, or that the underlying connector is being called with the appropriate parameters.

You could also use an in-process database for testing purposes so that it starts off with a "clean slate" every test run.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29261
    
140

Rakesh,
This is integration testing. (unit testing wouldn't involve a database)

There are two approaches:
1) Delete any records you inserted in the tear down
2) Use an in memory database as David suggested


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Rakesh Jhamb
Ranch Hand

Joined: Jun 18, 2003
Posts: 154
Thanks for your explanations. Are there any online resources, that can help in understanding best practices to be used in unit testing.

Please suggest.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29261
    
140

junit.org
xunitpatterns.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unit testing applications best practices.
 
Similar Threads
Is JUnit for j2ee ??
testing JSP applications with JUnit
Non J2EE transactional management in Java
Choosing frame work for UNIT test
Test-Driven Development