Announcement Announcement Module
Collapse
No announcement yet.
Junit DAO testcase problem - need help. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Junit DAO testcase problem - need help.

    Hi Folks,

    Here's a very simple test case that's throwing a HibernateMappingException.

    The same code works fine through the web app !!
    But it fails when run as a Junit test :-(

    Here's the exception I get when I run the test:

    ========== the exception ================
    [junit] Testcase: testNewUser(com.netblue.commons.login.UserTest): Caused an ERROR
    [junit] Unknown entity: com.netblue.commons.login.dao.User; nested exception is org.hibernate.MappingException: Unknown entity: com.netblue.commons.login.dao.User
    [junit] org.springframework.orm.hibernate3.HibernateSystem Exception: Unknown entity: com.netblue.commons.login.dao.User; nested exception is org.hibernate.MappingException: Unknown entity: com.netblue.commons.login.dao.User
    [junit] org.hibernate.MappingException: Unknown entity: com.netblue.commons.login.dao.User
    [junit] at org.hibernate.impl.SessionFactoryImpl.getEntityPer sister(SessionFactoryImpl.java:569)
    [junit] at org.hibernate.impl.SessionImpl.getEntityPersister( SessionImpl.java:1086)
    [junit] at org.hibernate.event.def.AbstractSaveEventListener. saveWithGeneratedId(AbstractSaveEventListener.java :83)
    [junit] at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.saveWithGeneratedOrRequestedId(DefaultSaveO rUpdateEventListener.java:184)
    [junit] at org.hibernate.event.def.DefaultSaveEventListener.s aveWithGeneratedOrRequestedId(DefaultSaveEventList ener.java:33)
    [junit] at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.entityIsTransient(DefaultSaveOrUpdateEventL istener.java:173)
    [junit] at org.hibernate.event.def.DefaultSaveEventListener.p erformSaveOrUpdate(DefaultSaveEventListener.java:2 7)
    [junit] at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.onSaveOrUpdate(DefaultSaveOrUpdateEventList ener.java:69)
    [junit] at org.hibernate.impl.SessionImpl.save(SessionImpl.ja va:481)
    [junit] at org.hibernate.impl.SessionImpl.save(SessionImpl.ja va:476)
    [junit] at org.springframework.orm.hibernate3.HibernateTempla te$12.doInHibernate(HibernateTemplate.java:615)
    [junit] at org.springframework.orm.hibernate3.HibernateTempla te.execute(HibernateTemplate.java:365)
    [junit] at org.springframework.orm.hibernate3.HibernateTempla te.save(HibernateTemplate.java:612)
    [junit] at com.netblue.commons.login.dao.UserDaoHibernate.add User(UserDaoHibernate.java:30)
    [junit] at com.netblue.commons.login.service.RegisterService. doRegister(RegisterService.java:20)
    [junit] at com.netblue.commons.login.UserTest.testNewUser(Use rTest.java:32)

    ====================================


    Here's the test code:

    ======== test code ======================
    public class UserTest extends TestCase {
    /**
    * setUp
    */
    public void setUp() {

    }

    /**
    * testIsAreacodeExisted
    */
    public void testNewUser() {
    String username = "test";
    String pw = "test123";
    String fullname = "Junit Test User";
    String email = "[email protected]";

    String paths =
    "com/netblue/commons/login/pp-hibernate-cfg.xml";
    ApplicationContext ctx =
    new ClassPathXmlApplicationContext(paths);

    UserDao dao = (UserDao) ctx.getBean("userDao");
    User user = new User(username, password, fullname, email);

    dao.getHibernateTemplate().save(user);

    // is the newly added user valid ?
    assertEquals("return value", user != null, true);
    }
    }
    =====================================



    and here's the hibernate config file:

    ======================================
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <!--
    - Application context definition for "example" DispatcherServlet.
    -->

    <beans>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName"><value>org.postgresql.Drive r</value></property>
    <property name="url"><value>jdbcostgresql://localhost:5432/portaldb</value></property>
    <property name="username"><value>postgres</value></property>
    <property name="password"><value>postgres123</value></property>
    </bean>

    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.PostgreSQLDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    <property name="mappingDirectoryLocations">
    <list>
    <value>classpath:/com/netblue/commons/login/dao</value>
    </list>
    </property>
    </bean>

    <bean id="hibernateTemplate"
    class="org.springframework.orm.hibernate3.Hibernat eTemplate">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    <bean id="userDao"
    class="com.netblue.commons.login.dao.UserDaoHibern ate">
    <property name="hibernateTemplate">
    <ref bean="hibernateTemplate"/>
    </property>
    </bean>

    <bean id="sessionDao"
    class="com.netblue.commons.login.dao.SessionDaoHib ernate">
    <property name="hibernateTemplate">
    <ref bean="hibernateTemplate"/>
    </property>
    </bean>

    </beans>

    ======================================

    Am I missing something in the test code ?
    The same code works perfectly fine thru the webapp.

    Thanks for your help.
    -anand

  • #2
    You have classpath problems - your mapping files are not found. Make sure they are there and try running the junit from the command line (or from ant) instead from the editor directly. Eclipse especially changes the classpath - you can do a simple class to test the files that are in your classpath and see if your mapping files are found.

    Comment


    • #3
      Check the fully qualified class names in your java source files and in the mapping files. Maybe the mapping files were not updated after refactoring the source (I had this).

      Greetings
      Juergen

      Comment

      Working...
      X