首页 > 代码库 > JUnit测试工具在项目中的用法

JUnit测试工具在项目中的用法

0:33 2013/6/26

三大框架整合时为什么要对项目进行junit测试:
    |__目的是测试配置文件对不对,能跑通就可以进行开发了

具体测试步骤:
    |__1.对hibernate进行测试 配置hibernate.cfg.xml
        public class TestHibernate {
               @Test
               public void save(){
        Configuration configuration = new Configuration();
        //加载类路径下的hibernate.cfg.xml
        configuration.configure();
        SessionFactory sf = configuration.buildSessionFactory();
        Session s = sf.openSession();
        Transaction tr = s.beginTransaction();
        //操作对象,就是操作表的过程
        ElecText elecText = new ElecText();
        elecText.setTextName("测试Hibernate名称");
        elecText.setTextDate(new Date());
        elecText.setTextRemark("测试Hibernate备注");
        s.save(elecText);
        tr.commit();
        s.close();
                }
        }
    |__2.对dao进行测试  测试之前确保beans.xml文件已配置,数据源可以暂时先不引不用开Tomcat不需要jsp进行显示
        public class TestDao {
               /**保存*/
               @Test
               public void save(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        IElecTextDao elecTextDao = (IElecTextDao) ac.getBean(IElecTextDao.SERVICE_NAME);
        ElecText elecText = new ElecText();
        elecText.setTextName("测试Dao名称");
        elecText.setTextDate(new Date());
        elecText.setTextRemark("测试DAO备注");
        elecTextDao.save(elecText);
                } 
                      }
    |__3.对service进行测试  测试之前确保beans.xml文件已配置,数据源可以暂时先不引不要开Tomcat不需要jsp进行显示
        public class TestService {
               @Test
               public void saveElecText(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        IElecTextService elecTextService = (IElecTextService) ac.getBean(IElecTextService.SERVICE_NAME);
        ElecText elecText = new ElecText();
        elecText.setTextName("测试Service名称");
        elecText.setTextDate(new Date());
        elecText.setTextRemark("测试Service备注");
        elecTextService.saveElecText(elecText);
               }
          }
    |__4.对二级缓存进行测试  配置hibernate.cfg.xml
        public class TestHibernateCache {
               /**测试类级别的缓存*/
               @Test
               public void classCache(){
        Configuration configuration = new Configuration();
        //加载类路径下的hibernate.cfg.xml
        configuration.configure();
        SessionFactory sf = configuration.buildSessionFactory();
        /***************************************************************/
        
        Session s = sf.openSession();
        Transaction tr = s.beginTransaction();
        
        ElecSystemDDL e1 = (ElecSystemDDL) s.get(ElecSystemDDL.class, 1);//产生select语句
        ElecSystemDDL e2 = (ElecSystemDDL) s.get(ElecSystemDDL.class, 1);//不产生select语句,从一级缓存中读取
        
        tr.commit();
        s.close();//一级缓存关闭了
        
        /********************************************************************/
        s = sf.openSession();
        tr = s.beginTransaction();
        
        ElecSystemDDL e3 = (ElecSystemDDL) s.get(ElecSystemDDL.class, 1);//不产生select语句,从二级缓存中读取
        
        tr.commit();
        s.close();
               }    
         }
    |__5.对jbpm进行测试  测试之前确保beans.xml文件已配置,数据源可以暂时先不引不要开Tomcat不需要jsp进行显示
        public class TestJbpm {
              /**生成JBPM的18张表*/
              @Test
              public void createTable_18(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        SessionFactory sf =  (SessionFactory) ac.getBean("sessionFactory");
        System.out.println("sf:"+sf);
               }
    
              /**测试流程引擎对象*/
              @Test
              public void testProcessEngine(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        ProcessEngine processEngine = (ProcessEngine) ac.getBean("processEngine");
        System.out.println("processEngine:"+processEngine);
               }
                     }