码迷,mamicode.com
首页 > Web开发 > 详细

hibernate 单元测试 5.2

时间:2015-04-28 17:45:06      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

 单元测试 测试 dao service action

package com.kaishengit.test;

import org.hibernate.Session;

import com.kaishengit.pojo.Account;
import com.kaishengit.util.HibernateUtil;

import junit.framework.TestCase;
//继承TestCase 
public class HibernateTest extends TestCase{

    
    private Session session = null;
    //测试之前要做的,这样就不用每次测试都要获取session 
    @Override
    protected void setUp() throws Exception {
        session = HibernateUtil.getSession();
    }
    //测试之后做的 
    @Override
    protected void tearDown() throws Exception {
        
    }

    
    /* 这是junit3系列的还不是使用注解,所以还都是要以 public void开头*/
    public void testAdd() {
        
        session.beginTransaction();
        
        Account a = new Account();
        a.setName("zzzz");
        session.save(a);
        
        session.getTransaction().commit();
    }
    
    public void testFindById() {
        session.beginTransaction();
        
        Account a = (Account) session.get(Account.class, "4028805e49a826140149a82615f70000");
        
        
        session.getTransaction().commit();
        
        assertNotNull(a);
        //Assert.assert
        //Assert.assertNotNull(a);
        //Assert.assertNull(a);
    }
    
    
    
}

run as junit test
不抛出异常或者跟预期一致都会显示绿颜色

hibernate 单元测试 5.2

标签:

原文地址:http://www.cnblogs.com/itliucheng/p/4463487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!