I want to test some new functionality which is part of an internal web app. This new code uses a database connection normally provided by an app server (tomcat).
I do not want to recreate the entire web app on my local machine to test the new code, since I only need to run one function.
Does anyone know how I can ‘spoof’ a Context, or Datasource, to retrieve the database config, without actually creating a web app instance on a server?
With the help of Spring SimpleNamingContextBuilder and Apache BasicDataSource, you can do something like this (I usually have this in a static block in test classes that need JNDI):
The value of
jndi_namemight look like this:java:comp/env/jdbc/my-dbOnce this is set up, code that normally looks up the database connection via JNDI should work. The code above would for example work with this Spring config: