Results 1 to 3 of 3

Thread: Unit test DAO using the same applicationContext.xml

  1. #1
    Join Date
    Mar 2006
    Posts
    22

    Default Unit test DAO using the same applicationContext.xml

    Hi

    I created applicationContext.xml and hibernate.cfg.xml in MyProject/WebContent/WEB-INF

    Code:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="configLocation" value="WEB-INF/hibernate.cfg.xml" />
      ......
    </bean>
    <bean id="userDao" class="com.myproject.dao.impl.UserDaoImpl">
      <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    This configuration works, and my web application is able to retrieve database records from userDao.

    Now, I created an unit test case for UserDao,
    Code:
    public class UserDaoTest {
        private ApplicationContext ctx;
        @Before
        public void setup() {
            ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        }
        ......
    }
    When run, it throws FileNotFoundException,

    Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/hibernate.cfg.xml] cannot be resolved to URL because it does not exist

    It seems that I cannot specify WEB-INF/hibernate.cfg.xml, but changing this will cause my application not able to find the file during runtime.

    Do you know how to resolve this problem?

  2. #2
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by wolverine View Post
    Do you know how to resolve this problem?
    I solved this by using a PropertyPlaceholderConfigurer. I have different properties for web server and test environment.

    Joerg
    This post can contain insufficient information.

  3. #3
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Just on a side note, if you are unit testing it might be a good idea to have a look at the spring helper classes.
    http://www.springframework.org/docs/...e/testing.html
    Last edited by karldmoore; Aug 27th, 2007 at 02:31 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •