File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unit testing applications best practices.

 
Rakesh Jhamb
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 33700
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rakesh Jhamb
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 33700
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
junit.org
xunitpatterns.com
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic