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 }