• 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

Tomcat datasource when using JUnit

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm am new to Junit so bare with me.
I'm using Junit test some classes that normally run on Tomcat. My problem is that these classes use a JNDI namespace resource (mysql database) specified in the Tomcat setup. I can't access these from my test class. How can I manually create these JNDI namespace match that on my Tomcat server?
TIA
[ December 18, 2002: Message edited by: Jeff Jensen ]
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you considered using a framework like
Cactus?

I was recently a beginner with JUnit as well, and Cactus can be slightly daunting at first. But pretty cool, because it runs "in container" and so your JNDI lookups will all work.

Their tutorial runs under Tomcat, and I found it quite useful. Unfortunately, their nice little stylesheet blew up on me.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just realized you said "class" but not servlet. If you're creating a bunch of helper classes, or Data Access classes that are then used by Servlets, and your classes themselves are dependant on JNDI stuff, perhaps this is an indication that the servlets should be passing in this information to the classes. In that way, you might be able to 're-use' those classes elsewhere (ie: not on the server-side)

Maybe I'm no architect though, and these type of implementation details always seem to escape me.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not counting jdo or ejb apps, I don't use JNDI to access the database connection pool directly. I use JRPoolManager. It's a simple wrapper class around configurable database pools. I have several implementations, one for JNDI, one for DbConnectionBroker from javaexchange and one for the special pool class provide by EJIP. The init of JRPoolManager takes a Properties Object and uses that to provide access to the pool. This way, when I want to test simple classes to acces a database, I pass the properties for the DbConnectionBroker and the class works. When it's running for JNDI, it passes the parameters need for that and...
I'm running a project on SourceForge for the Students that belong to our local JUG. The project name is jugsoft http://www.sourceforge.net/projects/jugsoft I use a version of it there. The project is still pretty young so the docs on how to use it aren't what they should be but, should give you an idea. BTW, there are property files and test cases that function.
 
Jeff Jensen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Carl,
That sounds like something I could use. I'll take a look at it.
Also thanks to the others for replying.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cactus is good for test groups of code in a webapp. However, for fine testing of a dataaccess object (one single read of a database row or testing the Foriegn key constraint protects your data), it's fairly heavy and involved to get a functional test. It would be nice to see a simple test helper that wrapped say the DbConnectionBroker pool with a DataSource and exposed it using the InitialContext so it could be found using JNDI.
I've seen reports of this for projects on SourceForge but, no code.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You sparked my interest. Here is a barebones JUnit Test Helper which can be setup to simulate a JNDI DataSource lookup and get a connection to the database. It creates a new connection each time one is ask for so...
http://www.javaranch.com/CodeBarn/jrunittesthelper.zip
The JavaDocs for JNDIUnitTestHelper give examples of it's use and it includes an example property file for setup.
[ December 20, 2002: Message edited by: Carl Trusiak ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic