新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论Java, J2SE, J2ME, J2EE, 以及Eclipse, NetBeans, JBuilder等Java开发环境,还有JSP, JavaServlet, JavaBean, EJB以及struts, hibernate, spring, webwork2, Java 3D, JOGL等相关技术。
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Java/Eclipse 』 → [分享]Testing Spring - Hibernate applications with Unitils 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 3477 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: [分享]Testing Spring - Hibernate applications with Unitils 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     hongjunli 帅哥哟,离线,有人找我吗?魔羯座1978-1-20
      
      
      威望:5
      头衔:为振兴论坛而努力!
      等级:研二(中了一篇WWWC Poster)(版主)
      文章:808
      积分:7964
      门派:IEEE.ORG.CN
      注册:2006/3/9

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给hongjunli发送一个短消息 把hongjunli加入好友 查看hongjunli的个人资料 搜索hongjunli在『 Java/Eclipse 』的所有贴子 引用回复这个贴子 回复这个贴子 查看hongjunli的博客楼主
    发贴心情 [分享]Testing Spring - Hibernate applications with Unitils

    Unitils is an interesting new open source project trying to make unit testing as easy and maintainable as possible. Together with their 1.0 RC1 release the Unitils team have published Testing Spring - Hibernate applications with Unitils which illustrates using Unitils in a typical Spring/Hibernate application. The way Unitils integrates with DbUnit and automagically does test database maintenance is particularly interesting.

    如何测试Spring - Hibernate , 首先是一个HibernatePhonebook 的类,继承了HibernateDaoSupport 从数据库读取Person信息

    public class HibernatePhonebook extends HibernateDaoSupport implements Phonebook {   
      
        public List<Person> searchByLastName(String lastName) {   
            return (List<Person>) getHibernateTemplate().executeFind(new HibernateCallback() {   
                    public Object doInHibernate(Session session) throws HibernateException, SQLException {   
                        Criteria personCriteria = session.createCriteria(Person.class);   
                        personCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);   
                        personCriteria.add(Restrictions.like("lastName", criteria.getLastName(), MatchMode.ANYWHERE));   
                        return personCriteria.list();   
                    }   
            });   
        }   
    }  
    public class HibernatePhonebook extends HibernateDaoSupport implements Phonebook {

        public List<Person> searchByLastName(String lastName) {
            return (List<Person>) getHibernateTemplate().executeFind(new HibernateCallback() {
                    public Object doInHibernate(Session session) throws HibernateException, SQLException {
                        Criteria personCriteria = session.createCriteria(Person.class);
                        personCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
                        personCriteria.add(Restrictions.like("lastName", criteria.getLastName(), MatchMode.ANYWHERE));
                        return personCriteria.list();
                    }
            });
        }
    } 它的测试也非常的简单,如下 view plaincopy to clipboardprint?
    @ApplicationContext({"classpath:services-config.xml", "classpath:test-ds-config.xml"})   
    public class UnitilsHibernatePhonebookTest extends UnitilsJUnit3 {   
           
        @SpringBeanByType  
        private Phonebook phonebook;   
      
        @DataSet  
        public void testSearchByLastName() {   
            List<Person> result = phonebook.searchByLastName("Doe"));           
            ReflectionAssert.assertPropertyLenEquals("firstName", Arrays.asList("John", "Jane"), result);       
        }   
    }  
    @ApplicationContext({"classpath:services-config.xml", "classpath:test-ds-config.xml"})
    public class UnitilsHibernatePhonebookTest extends UnitilsJUnit3 {
        
        @SpringBeanByType
        private Phonebook phonebook;

        @DataSet
        public void testSearchByLastName() {
            List<Person> result = phonebook.searchByLastName("Doe"));        
            ReflectionAssert.assertPropertyLenEquals("firstName", Arrays.asList("John", "Jane"), result);    
        }
    } 首先要继承UnitilsJUnit3 view plaincopy to clipboardprint?
    public class UnitilsHibernatePhonebookTest extends UnitilsJUnit3 {  
    public class UnitilsHibernatePhonebookTest extends UnitilsJUnit3 { 还要写annotation来读取入相应的spring配置 view plaincopy to clipboardprint?
    @ApplicationContext({"classpath:services-config.xml", "classpath:test-ds-config.xml"})  
    @ApplicationContext({"classpath:services-config.xml", "classpath:test-ds-config.xml"}) 通过Bean的属性来get bean,相信应该也有 ByName的方法 view plaincopy to clipboardprint?
    @SpringBeanByType  
    private Phonebook phonebook;  
    @SpringBeanByType
    private Phonebook phonebook; 以下是Spring的配置文件 view plaincopy to clipboardprint?
    <?xml version="1.0" encoding="UTF-8"?>  
    <beans>  
      
        <bean id="phonebook" class="org.unitils.sample.phonebook.impl.HibernatePhoneBook">  
            <property name="sessionFactory" ref="sessionFactoryBean"/>  
        </bean>  
      
        <bean id="sessionFactoryBean" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
            <property name="dataSource" ref="dataSource"/>  
            <property name="annotatedClasses">  
                <list>  
                    <value>org.unitils.sample.phonebook.domain.Person</value>  
                </list>  
            </property>  
            <property name="hibernateProperties">  
                <value>  
                    hibernate.dialect=org.hibernate.dialect.HSQLDialect   
                    hibernate.show_sql=true  
                </value>  
            </property>  
        </bean>  
           
    </beans>  
    <?xml version="1.0" encoding="UTF-8"?>
    <beans>

        <bean id="phonebook" class="org.unitils.sample.phonebook.impl.HibernatePhoneBook">
            <property name="sessionFactory" ref="sessionFactoryBean"/>
        </bean>

        <bean id="sessionFactoryBean" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="annotatedClasses">
                <list>
                    <value>org.unitils.sample.phonebook.domain.Person</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <value>
                    hibernate.dialect=org.hibernate.dialect.HSQLDialect
                    hibernate.show_sql=true
                </value>
            </property>
        </bean>
        
    </beans> 这里的datasource 在测试时暂时改为UnitilsDataSource view plaincopy to clipboardprint?
    <?xml version="1.0" encoding="UTF-8"?>  
    <beans>  
        <bean id="dataSource" class="org.unitils.database.UnitilsDataSource" />  
    </beans>  
    <?xml version="1.0" encoding="UTF-8"?>
    <beans>
        <bean id="dataSource" class="org.unitils.database.UnitilsDataSource" />
    </beans> @DataSet annotation 让方法从 中获取出测试模拟数据 view plaincopy to clipboardprint?
    @DataSet  
    public void testSearch() {  
    @DataSet
    public void testSearch() { view plaincopy to clipboardprint?
    <?xml version='1.0' encoding='UTF-8'?>  
    <dataset>  
        <PERSON FIRSTNAME="John" LASTNAME="Doe" />  
        <PERSON FIRSTNAME="Jane" LASTNAME="Doe" />  
        <PERSON FIRSTNAME="Jim"  LASTNAME="Smith" />  
    </dataset>  
    <?xml version='1.0' encoding='UTF-8'?>
    <dataset>
        <PERSON FIRSTNAME="John" LASTNAME="Doe" />
        <PERSON FIRSTNAME="Jane" LASTNAME="Doe" />
        <PERSON FIRSTNAME="Jim"  LASTNAME="Smith" />
    </dataset> 用java reflection来比较实际获得的值和期望值 view plaincopy to clipboardprint?
    ReflectionAssert.assertRefEquals(new Person("John", "Doe"), result.get(0));   
    ReflectionAssert.assertPropertyRefEquals("firstName", Arrays.asList("John", "Jane"), result);    
    ReflectionAssert.assertRefEquals(new Person("John", "Doe"), result.get(0));
    ReflectionAssert.assertPropertyRefEquals("firstName", Arrays.asList("John", "Jane"), result);   还附带一些其他的比较方式 view plaincopy to clipboardprint?
    ReflectionAssert.assertLenEquals(new Person("John", null), result.get(0));   
    ReflectionAssert.assertPropertyLenEquals("firstName", Arrays.asList("John", "Jane"), result);


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/4/7 9:02:00
     
     GoogleAdSense魔羯座1978-1-20
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Java/Eclipse 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/1/3 1:17:09

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    71.777ms