JUnit單元測試&簡版學生管理系統


JUnit的作用是:在龐大的程序項目中,要測試一個功能模塊,不需要將整個龐大的項目都運行,只需要將需要測試的功能塊進行JUnit測試就行

非常的方便,也很清晰,提高的開發的速度。

目前普遍使用的JUnit版本為JUnit4

JUnit的格式為:@Test  

可以利用快速修復鍵(ctrl+1),來添加JUnit4的jar包

在有多個JUnit測試存在時,直接右鍵運行會將 全部都執行,如果只需要測試一個功能,就先左鍵選中這個代碼塊,再右鍵運行。

JUnit單元測試的要求是:

權限修飾符必須是public  這是因為往往JUnit測試代碼會被寫在test包下,所以修飾符要為public,負責無法訪問

返回值類型為空。

沒有參數

 

正規的單元測試
1 》 在當前要測試的類上右鍵,選擇 new-- 》 junit test case
2 》 單元測試方法的命名:
test+ 要測試的方法的名稱

這是新建JUnit測試的步驟

隨機訪問流
1.RandomAccessFile :不屬於 IO 流體系的 IO 流,直接繼承 Object 類
用於 隨機訪問文件的讀寫。
2. 構造方法:
RandomAccessFile(File file, String mode) /RandomAccessFile(String name, String mode)
注意:
mode: 一共 4 種,常用兩種 r 表示只讀 ( 輸入流 ) rw 表示讀寫 ( 輸入 / 出流 )

代碼示例:

 1 public class  隨機訪問流 {
 2 public static void main(String[] args) throws IOException {
 3 RandomAccessFile raf=new RandomAccessFile(new File("1.txt"), "rw");
 4 // 寫 的方法
 5 /*raf.write("abc".getBytes());
 6 raf.write("def".getBytes());
 7 byte[] b=new byte[1024];
 8 int count = raf.read(b);
 9 System.out.println(new String(b,0,count));*/
10 // 隨機插入 seek(pointer) 設置文件指針位置。
11 raf.seek(1);
12 raf.write(" 我們 ".getBytes());
13 //getFilePointer  獲取指針當前位置。
14 System.out.println(raf.getFilePointer());
15 raf.close();
16 }
17 }

屬性集:

1.Properties : 屬性集,該屬性集中由鍵與值組成,並且都是字符串。繼承 HashTable ,屬於 Map 集合的一支

2. 構造方法:
Properties()
3. 常用方法:
1 》 load( 輸入流 ) 從輸入流中讀取屬性集
2 》 getProperty(String key) 通過 key 獲取值
4. 配置文件。 -- 》 .properties--- 》 xml

代碼示例:

 1 public class  屬性集 {
 2 public static void main(String[] args) throws IOException {
 3 // 數據庫? mysql mysql -- 》登錄 mysql -uroot -p123
 4 /*String username="root";
 5 String psw="123";*/
 6 // 創建屬性集對象
 7 //=========== 以下要求必會
 8 Properties prp=new Properties();
 9 /*System.out.println(prp);
10 prp.load(new FileInputStream(new File("Jdbc.properties")));
11 System.out.println(prp);
12 String name = prp.getProperty("username");
13 System.out.println(name);*/
14 //=========== 以上必會
15 prp.put("username", "root");
16 prp.put("psw", "123");
17 prp.store(new FileWriter(new File("3.properties")), " 這是一個 java 的文件 ");
18 }
19 }

 

 版本序列號就好比是一個模具。

 

 

下面是一個簡版的學生管理系統:

Student類

 

 1 package com.ujiuye.studentmanager;
 2 import java.io.Serializable;
 3 
 4 public class Student implements Serializable{
 5     /**
 6      * 序列化版本號
 7      */
 8     private static final long serialVersionUID = -9027709965061074374L;
 9     private String name;
10     private int age;
11     private String stuNo;
12     public String getName() {
13         return name;
14     }
15     public void setName(String name) {
16         this.name = name;
17     }
18     public int getAge() {
19         return age;
20     }
21     public void setAge(int age) {
22         this.age = age;
23     }
24     public String getStuNo() {
25         return stuNo;
26     }
27     public void setStuNo(String stuNo) {
28         this.stuNo = stuNo;
29     }
30     public Student() {
31         super();
32         // TODO Auto-generated constructor stub
33     }
34     public Student(String name, int age, String stuNo) {
35         super();
36         this.name = name;
37         this.age = age;
38         this.stuNo = stuNo;
39     }
40     @Override
41     public String toString() {
42         return "學生 :姓名 " + name + ", 年齡 " + age + ", 學號 " + stuNo ;
43     }
44 }

 

服務器端負責增刪改查的代碼

StudnetDao

  1 package com.ujiuye.studentmanager;
  2 
  3 import java.io.IOException;
  4 import java.util.ArrayList;
  5 import java.util.Iterator;
  6 import java.util.Scanner;
  7 
  8 import com.ujiuye.studentmanager.util.IOUtils;
  9 
 10 
 11 public class StudentDAO {
 12       /**
 13      *  思路:
 14      *   ① 輸入要添加的姓名  年齡  學號
 15      *   ② 首先從文件中讀取集合,再添加學生對象
 16      *   ③ 將該集合寫入到文件中。
 17      * @throws IOException 
 18      */
 19     public  void add(String fileName) throws IOException {
 20           Scanner sc=new Scanner(System.in);
 21           System.out.println("請輸入要添加的學生的姓名:");
 22           String name = sc.next();
 23           System.out.println("請輸入要添加的學生的年齡:");
 24           int age = sc.nextInt();
 25           System.out.println("請輸入要添加的學生的學號:");
 26           String stuNo = sc.next();
 27           //讀取
 28           ArrayList<Student> list = IOUtils.getStudents(fileName);
 29           //添加用戶輸入的學生對象
 30           list.add(new Student(name, age, stuNo));
 31           //寫入文件,再顯示
 32           IOUtils.writeToList(list, fileName);
 33           System.out.println("添加成功");
 34           query(fileName);
 35       }
 36       public  void delete(String fileName) throws IOException {
 37           Scanner sc=new Scanner(System.in);
 38           System.out.println("請輸入要刪除學生的學號:");
 39           String stuNo = sc.next();
 40           //查詢學號 ==》直接刪除,不判斷是否存在。
 41           //遍歷集合  刪除學生 ==》文件中讀取
 42           //建議刪除時 使用迭代器
 43           ArrayList<Student> list = IOUtils.getStudents(fileName);
 44           //判斷該集合是否存在元素
 45           if(!list.isEmpty()) {
 46               Iterator<Student> it = list.iterator();
 47               while(it.hasNext()) {
 48                   Student st = it.next();
 49                   if(st.getStuNo().equals(stuNo)) {
 50                       //刪除
 51                       it.remove();
 52                   }
 53               }
 54           }
 55           //寫入文件,並顯示
 56           IOUtils.writeToList(list, fileName);
 57           System.out.println("刪除成功");
 58           query(fileName);
 59       }
 60       public  void update(String fileName) throws IOException {
 61           Scanner sc=new Scanner(System.in);
 62           System.out.println("請輸入要修改的學生的學號:");
 63           String stuNo = sc.next();
 64           //遍歷集合,看學號是否存在
 65           //讀取
 66           ArrayList<Student> list = IOUtils.getStudents(fileName);
 67           //標識位  
 68           boolean flag=true;
 69           if(!list.isEmpty()) {
 70               Iterator<Student> it = list.iterator();
 71               while(it.hasNext()) {
 72                   Student st = it.next();
 73                   if(st.getStuNo().equals(stuNo)) {
 74                       System.out.println("請輸入要修改的學生的姓名:");
 75                       String name = sc.next();
 76                       System.out.println("請輸入要修改的學生的年齡:");
 77                       int age = sc.nextInt();
 78                       st.setName(name);
 79                       st.setAge(age);
 80                       flag=false;
 81                     //寫文件 再顯示
 82                          IOUtils.writeToList(list, fileName);
 83                          System.out.println("修改成功");
 84                          query(fileName);
 85                          break;// 找到一個學號 就修改
 86                   }
 87               }
 88           }
 89          if(flag) {
 90              System.out.println("沒有該學號存在");
 91          }
 92         
 93       }
 94       //查詢
 95       //思路: 讀取文件中的集合,遍歷 ,前提,判斷集合中是否存在元素,沒有 ,提示添加,然后再查詢。
 96       public  void query(String fileName) throws IOException {
 97           //讀取的方法 工具類
 98           ArrayList<Student> list=IOUtils.getStudents(fileName);
 99           //判斷
100           if(list.isEmpty()) {
101               System.out.println("沒有數據,請先添加再查詢");
102           }else {
103               //遍歷
104               Iterator<Student> it = list.iterator();
105               while(it.hasNext()) {
106                   System.out.println(it.next());
107               }
108           }
109       }
110 }

 

 

主方法:

 1 package com.ujiuye.studentmanager;
 2 
 3 import java.util.Scanner;
 4 
 5 
 6 public class Test_Main {
 7     public static void main(String[] args) {
 8         Scanner sc=new Scanner(System.in);
 9         //創建StudentDAO類的對象
10         StudentDAO std=new StudentDAO();
11         try {
12             while (true) {
13                 System.out.println("========歡迎光臨學生管理系統=============");
14                 System.out.println("添加學生 請按1 ,刪除學生 請按2 ,修改學生 請按3 ,查詢學生 請按4退出 請按5");
15                 //用戶的選擇
16                 int choose = sc.nextInt();
17                 switch (choose) {
18                 case 1:
19                     std.add("stu.txt");
20                     break;
21                 case 2:
22                     std.delete("stu.txt");
23                     break;
24                 case 3:
25                     std.update("stu.txt");
26                     break;
27                 case 4:
28                     std.query("stu.txt");
29                     break;
30                 case 5:
31                     System.exit(0);//退出jvm
32                 }
33             } 
34         } catch (Exception e) {
35             e.printStackTrace();
36         }
37     }
38 }

 

IO流的代碼塊

 

 1 // package com.ujiuye.studentmanager.util;
 2 import java.io.File;
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.io.ObjectInputStream;
 7 import java.io.ObjectOutputStream;
 8 
 9 import java.util.ArrayList;
10 import com.ujiuye.studentmanager.Student;
11 public class IOUtils {
12     //
13     /*思路:
14      *  ① 讀取文件 ?
15      *    1》 如果文件沒有 ,新建一個文件。
16      *    2》讀取該文件 ,創建反序列化流。讀取文件。
17      * 
18      */
19     public static  ArrayList<Student>  getStudents(String fileName) throws IOException{
20 //         如果文件沒有 ,新建一個文件。
21         File file=new File(fileName);
22         if(!file.exists()) {
23             file.createNewFile();
24         }
25         //創建一個集合,來存儲學生對象
26         ArrayList<Student> list=null;
27 //        讀取該文件 ,創建反序列化流。讀取文件。
28         try(
29                 ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file))
30         ){
31             list = (ArrayList<Student>) ois.readObject();
32         }catch (Exception e) {
33             //EOFException 如果有文件,但是文件中沒有內容,報該異常。
34             list=new ArrayList<>();
35         }
36         //返回集合
37         return list;
38     }
39     //
40     /*思路 :
41      * 前提:先將文件中的集合讀取 。然后再進行寫入
42      * ① 將list集合寫入到文件中。
43      * ② 序列化流寫入
44      * 
45      */
46     public  static void writeToList(ArrayList<Student> list,String fileName) {
47         try(
48             ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File(fileName)));
49         ){
50             oos.writeObject(list);
51         }catch (Exception e) {
52             e.printStackTrace();
53         }
54     }
55 }

 

下面為測試代碼

 1 package com.ujiuye.studentmanager;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import java.io.IOException;
 6 
 7 import org.junit.Test;
 8 
 9 public class StudentDAOTest {
10     StudentDAO std=new StudentDAO();
11     @Test
12     public void testAdd() throws IOException {
13         std.add("stu.txt");
14     }
15 
16     @Test
17     public void testDelete() throws IOException {
18         std.delete("stu.txt");
19     }
20 
21     @Test
22     public void testUpdate() throws IOException {
23         std.update("stu.txt");
24     }
25 
26     @Test
27     public void testQuery() throws IOException {
28         std.query("stu.txt");
29     }
30 
31 }

 

 

 1 package com.ujiuye.studentmanager.util;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import java.io.IOException;
 6 import java.util.ArrayList;
 7 
 8 import org.junit.Test;
 9 
10 import com.ujiuye.studentmanager.Student;
11 
12 public class IOUtilsTest {
13 
14     @Test
15     public void testGetStudents() throws IOException {
16         System.out.println(IOUtils.getStudents("stu.txt"));
17     }
18 
19     @Test
20     public void testWriteToList() {
21         ArrayList<Student>  list=new ArrayList<>();
22         list.add(new Student("zs", 19, "1001"));
23         list.add(new Student("zs1", 19, "1002"));
24         list.add(new Student("zs2", 19, "1003"));
25         IOUtils.writeToList(list, "stu.txt");
26         
27     }
28 
29 }

 

下面為另外一個版本

 1 package StudentManager1;
 2 
 3 import java.io.Serializable;
 4 
 5 public class Student implements Serializable{
 6 
 7     /**
 8      * 
 9      */
10     private static final long serialVersionUID = 4858356809294180650L;
11     private String name;
12     private String stuNo;
13     private int age;
14     public Student(String name, String stuNo, int age) {
15         super();
16         this.name = name;
17         this.stuNo = stuNo;
18         this.age = age;
19     }
20     public Student() {
21         super();
22     }
23     @Override
24     public String toString() {
25         return "學生   姓名:" + name + " 學號: " + stuNo + " 年齡: " + age ;
26     }
27     public String getName() {
28         return name;
29     }
30     public void setName(String name) {
31         this.name = name;
32     }
33     public String getStuNo() {
34         return stuNo;
35     }
36     public void setStuNo(String stuNo) {
37         this.stuNo = stuNo;
38     }
39     public int getAge() {
40         return age;
41     }
42     public void setAge(int age) {
43         this.age = age;
44     }
45     @Override
46     public int hashCode() {
47         final int prime = 31;
48         int result = 1;
49         result = prime * result + age;
50         result = prime * result + ((name == null) ? 0 : name.hashCode());
51         result = prime * result + ((stuNo == null) ? 0 : stuNo.hashCode());
52         return result;
53     }
54     @Override
55     public boolean equals(Object obj) {
56         if (this == obj)
57             return true;
58         if (obj == null)
59             return false;
60         if (getClass() != obj.getClass())
61             return false;
62         Student other = (Student) obj;
63         if (age != other.age)
64             return false;
65         if (name == null) {
66             if (other.name != null)
67                 return false;
68         } else if (!name.equals(other.name))
69             return false;
70         if (stuNo == null) {
71             if (other.stuNo != null)
72                 return false;
73         } else if (!stuNo.equals(other.stuNo))
74             return false;
75         return true;
76     }
77     
78     
79 }

 

 1 package StudentManager1;
 2 
 3 import java.io.IOException;
 4 import java.util.ArrayList;
 5 import java.util.Iterator;
 6 import java.util.Scanner;
 7 
 8 public class StudentDao {
 9 
10     public void add(String fileName) throws Exception{
11         System.out.println("添加學生");
12         Scanner sc=new Scanner(System.in);
13         System.out.println("請輸入你想輸入的學生的姓名");
14         String name=sc.next();
15         System.out.println("請輸入你想輸入的學生的學號");
16         String stuNo=sc.next();
17         System.out.println("請輸入你想輸入的學生的年齡");
18         int age=sc.nextInt();
19         IOUtils iu=new IOUtils();
20         ArrayList<Student> list=iu.getStudents(fileName);
21         list.add(new Student(name,stuNo,age));
22         iu.writeStudents(list, fileName);
23         System.out.println("添加成功");
24         query(fileName);
25         
26         
27     }
28     public void delete(String fileName) throws Exception, Exception {
29         System.out.println("刪除學生");
30         Scanner sc=new Scanner(System.in);
31         System.out.println("請輸入你想刪除的學生的學號:");
32         String input=sc.next();
33         IOUtils iu=new IOUtils();
34         ArrayList<Student> list=iu.getStudents(fileName);
35         Iterator<Student> it=list.iterator();
36         while(it.hasNext()) {
37             Student stu=it.next();
38             if(stu.getStuNo().equals(input)) {
39                 it.remove();break;
40             }else {
41                 System.out.println("數據未找到");
42             }
43         }
44         iu.writeStudents(list, fileName);
45         System.out.println("刪除成功");
46         query(fileName);    
47     }
48     public void change(String fileName) throws Exception, Exception {
49         System.out.println("更改學生");
50         Scanner sc=new Scanner(System.in);
51         System.out.println("請輸入你想更改的學生的學號");
52         String stuNo=sc.next();
53         IOUtils iu=new IOUtils();
54         ArrayList<Student> list=iu.getStudents(fileName);
55         Iterator<Student> it=list.iterator();
56         while(it.hasNext()) {
57             Student stu=it.next();
58             if(stu.getStuNo().equals(stuNo)) {
59                 System.out.println("請輸入你想更改的姓名");
60                 String name=sc.next();
61                 stu.setName(name);
62                 System.out.println("請輸入你想更改的年齡");
63                 int age=sc.nextInt();
64                 stu.setAge(age);
65                 
66                 iu.writeStudents(list, fileName);
67                 System.out.println("修改成功");
68                 query(fileName);
69                 break;
70             }else {
71                 System.out.println("未找到對應學號");
72             }
73             
74         }
75         
76     }
77     public void query(String fileName) throws Exception{
78         System.out.println("查詢信息");
79         IOUtils iu=new IOUtils();
80         ArrayList<Student> list=iu.getStudents(fileName);
81         if(list.isEmpty()) {
82             System.out.println("數據為空,無法查詢");
83         }else {
84             Iterator<Student> it=list.iterator();
85             while(it.hasNext()) {
86                 Student stu=it.next();
87                 System.out.println(stu);
88                 break;
89             }
90         }
91         
92         
93     }
94     public void exit() {
95         System.exit(0);
96     }
97 }

 

 

 1 package StudentManager1;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Student_Main {
 6 
 7     public static void main(String[] args) {
 8         Scanner sc=new Scanner(System.in);
 9         
10             while(true) {
11                 System.out.println("1增2刪3該4查");
12                 int input=sc.nextInt();
13                 
14                 try {
15                 StudentDao stud=new StudentDao();
16                 switch(input) {
17                 case 1:stud.add("stu.txt");break;
18                 case 2:stud.delete("stu.txt");break;
19                 case 3:stud.change("stu.txt");break;
20                 case 4:stud.query("stu.txt");break;
21                 case 5:stud.exit();break;
22                 }
23             }
24                 catch(Exception e) {
25                     e.printStackTrace();
26                 }
27         }
28         
29         
30     }
31 
32     
33 
34     
35 }
 1 package StudentManager1;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 import java.io.ObjectInputStream;
 8 import java.io.ObjectOutputStream;
 9 import java.util.ArrayList;
10 
11 public class IOUtils {
12 
13     public ArrayList<Student> getStudents(String fileName) throws IOException, Exception{
14         File file=new File(fileName);
15         
16         if(!file.exists()) {
17             file.createNewFile();
18         }
19         ArrayList<Student> list=null;
20         try (
21                 ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
22                 ){
23             list=(ArrayList<Student>) ois.readObject();
24         }catch(Exception e) {
25             e.printStackTrace();
26         }
27         
28         return list;
29         
30     }
31     
32     public void writeStudents(ArrayList<Student> list, String fileName) throws Exception{
33         
34         try(
35                 ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(fileName));
36                 ){    
37                 oos.writeObject(list);
38         }catch(Exception e) {
39             list=new ArrayList<>();
40         }
41         
42         
43     }
44 }

 

 

 1 package StudentManager1;
 2 
 3 import java.io.IOException;
 4 import java.util.ArrayList;
 5 
 6 import org.junit.jupiter.api.Test;
 7 
 8 class IOUtilsTest {
 9 
10     @Test
11     void testGetStudents() throws Exception {
12         IOUtils iu=new IOUtils();
13         System.out.println(iu.getStudents("stu.txt"));
14     }
15 
16     @Test
17     void testWriteStudents() throws Exception {
18         ArrayList<Student>  list=new ArrayList<>();
19         list.add(new Student("zs", "1001", 19));
20         list.add(new Student("zs1", "1002",19));
21         list.add(new Student("zs2", "1003", 19));
22         IOUtils iu=new IOUtils();
23         iu.writeStudents(list, "stu.txt");
24     }
25 
26 }
 1 package StudentManager1;
 2 
 3 import static org.junit.jupiter.api.Assertions.*;
 4 
 5 import org.junit.jupiter.api.Test;
 6 
 7 class StudentDaoTest {
 8     StudentDao stud=new StudentDao();
 9     @Test
10     void testAdd() throws Exception {
11         stud.add("stu.txt");
12     }
13     @Test
14     void testDelete() throws Exception {
15         stud.delete("stu.txt");
16     }
17     @Test
18     void testChange() throws Exception {
19         stud.change("stu.txt");
20     }
21     @Test
22     void testQuery() throws Throwable {
23         stud.query("stu.txt");
24     }
25     @Test
26     void testExit() {
27         stud.exit();
28     }
29 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM