[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

JUnit Tests against a class that uses Tomcat Connection Pools

Posted on 2006-11-14
3
Medium Priority
?
1,748 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1500 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

650 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