Solved

JUnit Tests against a class that uses Tomcat Connection Pools

Posted on 2006-11-14
3
1,695 Views
Last Modified: 2013-11-24
I've got a class that normally executes from within Tomcat which uses a MySQL database connection pool..
I'd like to set up some JUnit tests to test this external to Tomcat.  

Currently, if I execute the test, I get the following exception:
ERROR: Unable to get a connection from jdbc/portalDb
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
        at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
        at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
        at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
        ...

I've read up on the jndi.properties file.  But the examples are horrible, I'm not sure what to set the java.naming.factory.initial property to, let alone how to define a connection to the MySQL database.  
How do I get these tests to work?

Thanks.
0
Comment
Question by:sasdevelopment
3 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17940631
jndi.properties file should be there on the classpath.
0
 
LVL 9

Accepted Solution

by:
owenli27 earned 500 total points
ID: 17940920
Error message indicates that you need to specify the type and location of your jndi store.   You may need to create a jndi.properties file and to specifying environment Properties in it:
java.naming.factory.object=xxxx.yyy.zzzz
java.naming.factory.state=xxxx.yyy.zzzz
java.naming.factory.initial=xxxx.yyy.zzzz
....
Then you have to make sure your jndi.properties file is in your CLASSPATH.

If you have permission to temporarily change code, a simple way is to change dataSource connection to:
Class.forName("org.gjt.mm.mysql.Driver");
String connectionURL = "jdbc:mysql://localhost:3306/mydatabase";
Connection connection = DriverManager.getConnection(connectionURL, "root", "password");
0
 

Author Comment

by:sasdevelopment
ID: 17942184
Thanks to those who responded.  I did some further digging and found a similar situation that someone else had.  I ended up creating a skeleton class that implements javax.naming.spi.InitialContextFactory and used that to create a connection to the database.  It works great and doesn't intrude on the source code.  

jndi.properties basically has one line:
java.naming.factory.initial=com.sungard.sase.portal.webapp.db.pool.ContextFactory

which points to my class.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strDist challenge 35 84
mapBully challenge 6 89
pairs challenge 5 44
What is the latest versions eclipse neon 2 120
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now