?
Solved

JUnit Tests against a class that uses Tomcat Connection Pools

Posted on 2006-11-14
3
Medium Priority
?
1,729 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

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

743 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