1、下载 JUnit,这里用JUnit 4.7

下载链接: http://pan.baidu.com/s/1c23n7LQ 密码: i18e

2、可以直接 build path 引入;也可以创建 User Library,然后引入Project中;

3、新建 Source Folder用来保存测试类

搭建JUnit环境

4、取名和要测试的类所在的文件夹名字一样,比如要测试的类所在文件夹是 com.bjsxt.hibernate.model,那取名也叫 com.bjsxt.hibernate.model;

搭建JUnit环境

5、要测试哪个类,测试类的就叫 被测试类名Test,比如:TeacherTest

6、TeacherTest

 1 package com.bjsxt.hibernate.model;
 2 
 3 import java.util.Date;
 4 
 5 import org.hibernate.Session;
 6 import org.hibernate.SessionFactory;
 7 import org.hibernate.cfg.AnnotationConfiguration;
 8 import org.junit.AfterClass;
 9 import org.junit.BeforeClass;
10 import org.junit.Test;
11 
12 public class TeacherTest {
13     private static SessionFactory sf = null;
14     
15     /**
16      * 类对象执行前
17      */
18     @BeforeClass
19     public static void beforeClass(){
20         sf = new AnnotationConfiguration().configure().buildSessionFactory();
21     }
22     
23     /**
24      * 类对象执行后
25      */
26     @AfterClass
27     public static void afterClass(){
28         if(sf != null){
29             sf.close();
30         }
31     }
32     
33 
34     @Test
35     public void testTeacherSave(){
36         Teacher t = new Teacher();
37         t.setId(1);
38         t.setName("s1");
39         t.setTitle("中级");
40         t.setBirthday(new Date());
41         
42         Session session = sf.openSession();
43         session.beginTransaction();
44         
45         session.save(t);
46         
47         session.getTransaction().commit();
48         session.close();
49     }
50 
51 }

代码:链接: http://pan.baidu.com/s/1i4CWAUh 密码: u865

所需jar包:

1:链接: http://pan.baidu.com/s/1miqgeH6 密码: 2bwx

2:Junit4下载链接: http://pan.baidu.com/s/1c23n7LQ 密码: i18e

相关文章:

  • 2021-11-12
  • 2022-12-23
  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
  • 2022-12-23
  • 2021-12-05
猜你喜欢
  • 2021-06-02
  • 2021-11-22
  • 2021-11-30
  • 2021-06-25
  • 2021-10-08
  • 2021-04-14
  • 2021-10-11
相关资源
相似解决方案