R
Rahul
Hi all,
In my web application (deployed in tomcat) I am using JNDI for fetching
database connections.
I did this by defining a context in META-INF/context.xml of my
application.
Here is what lies in my context.xml file:
<Context path="/myApp" docBase="myApp" debug="5" reloadable="true"
privileged="true" crossContext="true">
<Resource name="jdbc/myApp" auth="Container"
type="javax.sql.DataSource" maxActive="30" maxIdle="10"
maxWait="6000"
username="myApp_user" password="myApp_password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myDatabase"
removeAbandoned="true"
autoReconnect="true"
/>
</Context>
And this is the code I use to get a connection :
Context ctx = (Context) new InitialContext().lookup("java:comp/env");
if (ctx == null)
throw new Exception("No context available");
Connection connection = ((DataSource) ctx.lookup("jdbc/myApp"))
.getConnection();
It is working fine for me.
But now I want to write some unit tests of my DatabaseService classes
which
uses the above code to get a database connection.
But I don't know how to load the above context parameter, in a junit
test
case.
because without doing this I wont be able to get a database connection.
Any Suggestions ?
In my web application (deployed in tomcat) I am using JNDI for fetching
database connections.
I did this by defining a context in META-INF/context.xml of my
application.
Here is what lies in my context.xml file:
<Context path="/myApp" docBase="myApp" debug="5" reloadable="true"
privileged="true" crossContext="true">
<Resource name="jdbc/myApp" auth="Container"
type="javax.sql.DataSource" maxActive="30" maxIdle="10"
maxWait="6000"
username="myApp_user" password="myApp_password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myDatabase"
removeAbandoned="true"
autoReconnect="true"
/>
</Context>
And this is the code I use to get a connection :
Context ctx = (Context) new InitialContext().lookup("java:comp/env");
if (ctx == null)
throw new Exception("No context available");
Connection connection = ((DataSource) ctx.lookup("jdbc/myApp"))
.getConnection();
It is working fine for me.
But now I want to write some unit tests of my DatabaseService classes
which
uses the above code to get a database connection.
But I don't know how to load the above context parameter, in a junit
test
case.
because without doing this I wont be able to get a database connection.
Any Suggestions ?