• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unit testing applications best practices.

 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
junit.org
xunitpatterns.com
 
reply
    Bookmark Topic Watch Topic
  • New Topic