Solved

JUnit Tests against a class that uses Tomcat Connection Pools

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java asynchronous logging 4 50
reverse digits of a number using for loop 5 36
servlet filter example 37 46
tomcat startup error 5 21
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

864 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

20 Experts available now in Live!

Get 1:1 Help Now