dcsimg
testing web app data access methods using JUnit
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   David_Chelimsky
Posted On:   Saturday, July 20, 2002 08:17 AM

I'm using Ant and JUnit to build and unit test a web app that uses JSPs and Servlets. When running the application, the container makes a connection pool available via JNDI. I'm trying to use a DAO pattern in which the interface to business objects exposes persistence methods (save, restore, remove) but does not expose implementation details like JDBC Connections. Because the container provides a connection pool, accessible via JNDI, I can easily accomplish this within the container, but testing is another matter. I don't have any experience configuring a JNDI Context, so I've added methods to my classes that allow you to set a Connection in the DAO through the business object. I'd very much like to eliminate that for a var   More>>

I'm using Ant and JUnit to build and unit test a web app that uses JSPs and Servlets. When running the application, the container makes a connection pool available via JNDI.

I'm trying to use a DAO pattern in which the interface to business objects exposes persistence methods (save, restore, remove) but does not expose implementation details like JDBC Connections.

Because the container provides a connection pool, accessible via JNDI, I can easily accomplish this within the container, but testing is another matter. I don't have any experience configuring a JNDI Context, so I've added methods to my classes that allow you to set a Connection in the DAO through the business object. I'd very much like to eliminate that for a variety of reasons (excess code to maintain, security, etc).

Questions:


  1. Am I barking up the wrong tree? Should I just stay w/ allowing connections to be passed in?

  2. Is there anything on the market (commericial or open source) that will manage this for me within JUnit tests?



Thanks,
David    <<Less

Re: testing web app data access methods using JUnit

Posted By:   Anonymous  
Posted On:   Friday, August 16, 2002 12:08 PM

I don't know much about the DAO pattern, but it seems to me that it complicates testing by violating the Single Responsibility Rule (stating that there shouldn't be change for more than one reason - in this case it seems it could change because of changes in the database and because of changes in business rules). If database access would be handled by a different class, you could mock that easily for most of your tests.

The following links may help you in addition:


  • http://c2.com/cgi/wiki?UnitTestsAndDatabases
  • http://www.objectmentor.com/resources/articles/srp
  • http://c2.com/cgi/wiki?MockObject
About | Sitemap | Contact