Announcement Announcement Module
No announcement yet.
Junit DAO testcase problem - need help. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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(
    [junit] at org.hibernate.impl.SessionImpl.getEntityPersister(
    [junit] at org.hibernate.event.def.AbstractSaveEventListener. saveWithGeneratedId( :83)
    [junit] at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.saveWithGeneratedOrRequestedId(DefaultSaveO
    [junit] at org.hibernate.event.def.DefaultSaveEventListener.s aveWithGeneratedOrRequestedId(DefaultSaveEventList
    [junit] at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.entityIsTransient(DefaultSaveOrUpdateEventL
    [junit] at org.hibernate.event.def.DefaultSaveEventListener.p erformSaveOrUpdate( 7)
    [junit] at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.onSaveOrUpdate(DefaultSaveOrUpdateEventList
    [junit] at va:481)
    [junit] at va:476)
    [junit] at org.springframework.orm.hibernate3.HibernateTempla te$12.doInHibernate(
    [junit] at org.springframework.orm.hibernate3.HibernateTempla te.execute(
    [junit] at org.springframework.orm.hibernate3.HibernateTempla
    [junit] at com.netblue.commons.login.dao.UserDaoHibernate.add User(
    [junit] at com.netblue.commons.login.service.RegisterService. doRegister(
    [junit] at com.netblue.commons.login.UserTest.testNewUser(Use


    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 =
    ApplicationContext ctx =
    new ClassPathXmlApplicationContext(paths);

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


    // 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" "">

    - Application context definition for "example" DispatcherServlet.

    <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 id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource"/>
    <property name="hibernateProperties">
    <prop key="hibernate.dialect">
    <prop key="hibernate.show_sql">true</prop>
    <property name="mappingDirectoryLocations">

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

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

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



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

    Thanks for your help.

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


    • #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).