Java实训日志4
程序员文章站
2024-01-25 19:53:22
...
小结
上次课我创建了四个接口,创建了学校数据访问接口实现类,创建了教师数据访问接口实现类,并编写了两个测试类完成他们的测试。本次课的实训内容是继续完成创建数据访问接口实现类。分别创建学生数据访问接口实现类和用户数据访问接口实现类,本次课我完成了学生数据接口实现类和他对应的测试代码。
编写测试方法:
以下是TestStudentDaoImpl类的部分方法(这部分是按照老师博客上的写的)
package net.jxm.student.test;
import net.jxm.student.bean.Student;
import net.jxm.student.dao.Impl.StudentDaoImpl;
import net.jxm.student.dao.StudentDao;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class TestStudentDaoImpl {
StudentDao dao = new StudentDaoImpl();
@Test
public void testInsert() {
//创建学生对象
Student student = new Student();
student.setId("19242094");
student.setName("张晓慧");
student.setSex("女");
student.setAge(19);
student.setDepartment("艺术传媒学院");
student.setClazz("2019数媒3班");
student.setTelephone("15890674568");
int count = dao.insert(student);
if(count > 0) {
System.out.println("恭喜,学生记录插入成功!");
System.out.println(dao.findById(student.getId()));
}else {
System.out.println("遗憾,学生记录插入失败!");
}
}
@Test
public void testDeleteById() {
String id = "19242094";
int count = dao.deleteById(id);
if(count > 0) {
System.out.println("恭喜,学生记录删除成功!");
}else {
System.out.println("遗憾,学生记录删除失败!");
}
}
@Test
public void testDeleteByClazz() {
String clazz = "2019数媒3班";
int count = dao.deleteByClass(clazz);
if (count>0) {
System.out.println("恭喜,["+clazz+"]学生记录删除成功!");
}else{
System.out.println("遗憾,["+clazz+"]学生记录删除失败!");
}
}
@Test
public void testDeleteByName() {
String name = "李";
List<Student> students = dao.findByName(name);
if (students.size()>0) {
for (Student student :students) {
System.out.println(student);
}
}else{
System.out.println("温馨提示:查无此人!");
}
}
@Test
public void testFindAll() {
List<Student> students = dao.findAll();
for (Student student:students){
System.out.println(student);
}
}
@Test
public void testFindRowsBySex(){
Vector rows = dao.findRowsBySex();
Iterator iterator = rows.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
独立完成了剩余方法的测试任务
(1)编写测试方法testDeleteByDepartment()
(2)编写测试方法testUpdate()
(3)编写测试方法testFindById()
(4)编写测试方法testFindByClass()
(5)编写测试方法testFindByDepartment()
(6)编写测试方法testFindRowsByClass()
(7)编写测试方法testFindRowsByDepartment()