JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework)。Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能。Junit是一套框架,继承TestCase类,就可以用Junit进行自动测试了。
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class TestMethod {
static SessionFactory factory; // 全局静态工厂类,只创建一个
Session session; // 会话类,每个方法创建一个
Transaction tx; // 事务
// 所有方法运行之前运行一次
@BeforeClass
public static void setUpBeforeClass() throws Exception {
factory = new Configuration().configure().buildSessionFactory();
}
// 所有方法运行之后运行一次
@AfterClass
public static void tearDownAfterClass() throws Exception {
factory.close();
}
// 每个测试方法运行之前运行一次
@Before
public void setUp() throws Exception {
session = factory.openSession();
tx = session.beginTransaction();
}
// 每个测试方法运行之后运行一次
@After
public void tearDown() throws Exception {
tx.commit();
session.close();
}
/**
* 查询所有包含“园”的房子,面积在100到200之间
*/
//测试方法
@Test
public void test() {
String hql = "from House h where h.title like '%园%' and h.floorage between 100 and 200";
Query query = session.createQuery(hql);
List<House> houses = query.list();
for (House h : houses) {
String typeString = h.getHouseType().getName();
String nameString = h.getUsers().getName();
System.out.println("---------------------");
System.out.println("房屋:" + h.getTitle());
System.out.println("面积:" + h.getFloorage());
System.out.println("类型:" + typeString);
System.out.println("租金:" + h.getPrice());
System.out.println("出租人:" + nameString);
System.out.println("---------------------");
}
}
}