junit4.7
一、引入jar包
好的习惯,建立自己的user library
junit-4.7.jar
建议:
1.学习maven的做法,将测试代码放在test目录下
----new source folder
src放的是开发性的代码
2.对哪个类进行测试,就对哪个包下面建一个TeacherTest类
当然可以用myeclipse直接进行建立junit test case
要加@Test注解
二、进行生成测试类
TeacherTest.java
将代码修饰一下
(每次测试都要建立一次SessionFactory,很费时
----处理方式:
单例/static语句块
辅助的语句块放在static
另一个方法:
(hibernate建议的做法是)启动和辅助类
)
三、@BeforeClass在对象初始化之前,class load内存之后之后执行的方法
单元测试现在写的不够好
使用assert,就不用在数据库中手动查看
需要spring和hibernate结合。
补充知识:
注解:
写给编译器看的javac
运行环境看的java.exe运行的时候帮你做些事情。
代码案例:
package com.zhuhw.hibernate.model;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class TeacherTest {
/*这个类TeacherTest一进来就被初始化了,在测试方法执行用已经初始化好的SessionFactory;
* 只初始化一次*/
public static SessionFactory sf = null;
@BeforeClass
public static void beforeClass(){
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
@Test
public void TestTeacherSave(){
Teacher t = new Teacher();
t.setId(6);
t.setName("zhuhw2");
t.setTitle("ccc2");
//因为使用的annotation,所以Configuration要使用AnnotationConfiguration
/*Configuration cf = new AnnotationConfiguration();
SessionFactory sf = cf.configure().buildSessionFactory();*/
Session session = sf.openSession();
//在hibernate中执行操作要在一个事务里面
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
}
@AfterClass
public static void afterClass(){
sf.close();
}
}
由于一直是配置错误,原因是自己改了Teacher类的位置,增加了com.zhuhw.hibernate.model中的model,作用一直运行不成功
最后通过,原来的/hibernate_0100_HelloWorld/src/com/zhuhw/hibernate/model/TeacherOldTest.java中的报错信息进行解决了。
使用junit进行单元测试成功执行。
四、注意点
/hibernate_0100_HelloWorld/src/hibernate.cfg.xml
这个文件配置写错了,junit会有时出现把bug给吞了
解决办法:
在
sf = new AnnotationConfiguration().configure().buildSessionFactory();
加上try{} catch()
错误信息就会提示出来了。
代码案例:
package com.zhuhw.hibernate.model;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class TeacherTest {
/*这个类TeacherTest一进来就被初始化了,在测试方法执行用已经初始化好的SessionFactory;
* 只初始化一次*/
public static SessionFactory sf = null;
@BeforeClass
public static void beforeClass(){
/*关于junit会出现吞掉bug,1.可以在下面的语句上加上try catch
* 2.或者加个main()方法,在main里面进行调用 beforeClass()*/
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
@Test
public void TestTeacherSave(){
Teacher t = new Teacher();
t.setId(11);
t.setName("zhuhw7");
t.setTitle("ccc7");
//因为使用的annotation,所以Configuration要使用AnnotationConfiguration
/*Configuration cf = new AnnotationConfiguration();
SessionFactory sf = cf.configure().buildSessionFactory();*/
Session session = sf.openSession();
//在hibernate中执行操作要在一个事务里面
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
}
/*关于junit会出现吞掉bug,1.可以在下面的语句上加上try catch
* 2.或者加个main()方法,在main里面进行调用 beforeClass()*/
public void main() {
beforeClass();
}
@AfterClass
public static void afterClass(){
sf.close();
}
}