Announcement Announcement Module
Collapse
No announcement yet.
Unit test DAO using the same applicationContext.xml Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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, 02:31 PM.

      Comment

      Working...
      X