Solved

JUnit Tests against a class that uses Tomcat Connection Pools

Posted on 2006-11-14
3
1,705 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 62
hibernate example using maven 12 56
Adding multiple JVM environments to RedHat 6 7 46
What browser will run Java? 7 42
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

808 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