Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8413149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:51:51+00:00 2026-06-10T00:51:51+00:00

I am toying a bit with Spring testing framework, but I have one problem.

  • 0

I am toying a bit with Spring testing framework, but I have one problem. Normally when application is deployed on Tomcat we have

<Resource
       name="jdbc/sqliteDS"
       auth="Container"
       type="javax.sql.DataSource"
       maxActive="4"
       maxIdle="2"
       username="x"
       maxWait="5000"
       driverClassName="org.sqlite.JDBC"
       password="x"
       url="jdbc:sqlite:/home/xxx/db.sqlite"/> 

</Context>

in Tomcat context.xml,

<resource-ref>
    <description>sqlite DataSource</description>
    <res-ref-name>jdbc/sqliteDS</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

in web.xml and

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:/comp/env/jdbc/sqliteDS" />
</bean>

in data-context.xml
for getting data source, but how can I emulate JNDI resource for Spring test framework, because now during the initialization I am getting errors that data source is not found, and he is right.

Also, it would be great if one can do that without writing another .xml file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:51:52+00:00Added an answer on June 10, 2026 at 12:51 am

    I had to deal with this question a while ago, and I didn’t find a suitable solution, but a workaroud which implies another xml file :

    First you create a Spring configuration file defining your JNDI infos (jndi.xml) :

    <beans
      xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
      <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@server:port:instance" />
        <property name="username" value="user" />
        <property name="password" value="pwd" />
      </bean>
    </beans>
    

    Then a static class to bind your JNDI variable :

    public class Initialize {
        public static void initializeJndi() throws Exception {
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("jndi.xml");
            SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
            builder.bind("java:comp/env/jdbc/foo", applicationContext.getBean("dataSource"));
            builder.activate();
        }
    }
    

    Then in your test class, you add the following :

        @BeforeClass
        public static void initJndi() throws Exception {
            Initialize.initializeJndi();
        }
    

    So that when you load your Spring main configuration file, the JNDI resource is accessible.

    Maybe this is not the best way to do it but it surely works.

    By the way, having a specific configuration file seems to be a good idea because you may not want to run your unit test on the final database. Doing so is more considered as integration testing than unit testing.

    Hope it helps,

    Mouwah

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a bit of trouble doing some unit-testing using moq. If I have
I have been having quite a bit of trouble implementing unit testing on the
In short, I'm trying to learn a bit of Spring. Problem is, when I
Trying to get a bit more out of the command line: I have a
Im trying to learn a bit about c++ and have run in to some
I have a bit of JavaScript code that is specified in a configuration file
I'm trying to get a simple REST service to work with Spring 3.0 but
I am trying to upload a File with one parameter using spring 3. This
I am trying to build spring framework (both trunk and 3.0.5 Release). The build
I'm using ubuntu 64-bit and trying to run a .asm file on NASM. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.