hibernate 单元测试

时间:2023-03-09 01:12:17
hibernate 单元测试

 单元测试 测试 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
不抛出异常或者跟预期一致都会显示绿颜色