继续完成上午没有写完的测试类和用户接口。
独立完成剩余方法的测试任务
(1)编写测试方法testAddStudent()
@Test
public void testAddStudent() {
//创建学生对象
Student student = new Student();
//设置学生对象属性
student.setId("19205213");
student.setName("林秒秒");
student.setSex("女");
student.setAge(20);
student.setDepartment("艺术传媒学院");
student.setClazz("18数媒4班");
student.setTelephone("13890673428");
//调用学生数据访问对象的插入方法,
int count = service.addStudent(student);
//判断学生记录是否插入成功
if (count > 0) {
System.out.println("恭喜,学生记录插入成功!");
System.out.println(service.findStudentById(student.getId()));
} else {
System.out.println("遗憾,学生记录插入失败!");
}
}
(2)编写测试方法testDeleteStudentById()
@Test
public void testDeleteStudentById() {
String id = "18205226";
//调用学生数据访问对象的按id删除方法,
int count = service.deleteStudentById(id);
//判断学生记录是否删除成功
if (count > 0) {
System.out.println("恭喜,学生记录删除成功!");
} else {
System.out.println("遗憾,学生记录删除失败!");
}
}
(3)编写测试方法testDeleteStudentsByClass()
@Test
public void testDeleteStudentsByClass() {
String clazz = "18小教3班";
//调用学生数据访问对象的按班级删除方法,
int count = service.deleteStudentsByClass(clazz);
//判断学生记录是否删除成功
if (count > 0) {
System.out.println("恭喜,[" + clazz + "]学生记录删除成功!");
} else {
System.out.println("遗憾,[" + clazz + "]学生记录删除失败!");
}
}
(4)编写测试方法testDeleteStudentsByDepartment()
@Test
public void testDeleteStudentsByDepartment() {
String department = "商学院";
//调用学生数据访问对象的按学院删除方法,
int count = service.deleteStudentsByDepartment(department);
//判断学生记录是否删除成功
if (count > 0) {
System.out.println("恭喜,[" + department + "]学生记录删除成功!");
} else {
System.out.println("遗憾,[" + department + "]学生记录删除失败!");
}
}
(5)编写测试方法testUpdateStudent()
@Test
public void testUpdateStudent() {
//调用学生数据访问对象的查询方法
Student student = service.findStudentById("18205224");
//修改学生属性
student.setName("程晓心");
student.setSex("女");
student.setAge(19);
student.setDepartment("艺术传媒学院");
student.setClazz("18数媒4班");
student.setTelephone("15536485689");
//调用学生数据访问对象的更新方法
int count = service.updateStudent(student);
//判断学生记录是否更新成功
if (count > 0) {
System.out.println("学生记录更新成功!");
} else {
System.out.println("学生记录更新失败!");
}
}
(6)编写测试方法testFindStudentById()
@Test
public void testFindStudentById() {
//调用学生数据访问对象的查询方法
Student student = service.findStudentById("18205227");
//输出学生信息
System.out.println("id:" + student.getId());
System.out.println("姓名:" + student.getName());
System.out.println("性别:" + student.getSex());
System.out.println("年龄:" + student.getAge());
System.out.println("系部:" + student.getDepartment());
System.out.println("班级:" + student.getClazz());
System.out.println("电话:" + student.getTelephone());
}
(7)编写测试方法testFindAllStudents()
@Test
public void testFindAllStudents() {
//调用学生数据访问对象的查找全部方法,
List<Student> students = service.findAllStudents();
//通过增强for循环遍历学生列表
for (Student student : students) {
System.out.println(student);
}
}
(8)编写测试方法testFindStudentsByClass()
@Test
public void testFindStudentsByClass() {
//调用学生数据访问对象的查询方法
List<Student> students = service.findStudentsByClass("17英教1班");
//输出学生信息
for (Student student : students) {
System.out.println("id:" + student.getId());
System.out.println("姓名:" + student.getName());
System.out.println("性别:" + student.getSex());
System.out.println("年龄:" + student.getAge());
System.out.println("系部:" + student.getDepartment());
System.out.println("班级:" + student.getClazz());
System.out.println("电话:" + student.getTelephone());
}
}
(9)编写测试方法testFindStudentsByDepartment()
@Test
public void testFindStudentsByDepartment() {
//调用学生数据访问对象的查询方法
List<Student> students = service.findStudentsByDepartment("郎酒学院");
//判断列表里是否有元素
if (students.size() > 0) {
//通过增强for循环遍历列表
for (Student student : students) {
System.out.println("id:" + student.getId());
System.out.println("姓名:" + student.getName());
System.out.println("性别:" + student.getSex());
System.out.println("年龄:" + student.getAge());
System.out.println("系部:" + student.getDepartment());
System.out.println("班级:" + student.getClazz());
System.out.println("电话:" + student.getTelephone());
}
} else {
System.out.println("温馨提示:没有这个系部");
}
}
(10)编写测试方法testFindRowsBySex()
@Test
public void testFindRowsBySex() {
//调用学生数据访问对象的按性别统计人数方法,
Vector rows = service.findRowsBySex();
//获取向量的迭代器
Iterator iterator = rows.iterator();
//遍历迭代器
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
(11)编写测试方法testFindRowsByClass()
@Test
public void testFindRowsByClass() {
//调用学生数据访问对象的按班级统计人数方法,
Vector rows = service.findRowsByClass();
//获取向量的迭代器
Iterator iterator = rows.iterator();
//遍历迭代器
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
(12)编写测试方法testFindRowsByDepartment()
@Test
public void testFindRowsByDepartment() {
//调用学生数据访问对象的按学院统计人数方法,
Vector rows = service.findRowsByDepartment();
//获取向量的迭代器
Iterator iterator = rows.iterator();
//遍历迭代器
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
package net.jxm.student.service;
import net.jxm.student.bean.User;
import java.util.List;
/**
* 功能:用户服务接口
* 作者:金心敏
* 日期:2020.7.10
*/
public interface UserService {
int addUser(User user);
int deleteUserById(int id);
int updateUser(User user);
User findUserById(int id);
List<User> findAllUsers();
User login(String username, String password);
boolean isUsernameExisted(String username);
}
package net.jxm.student.service.impl;
import net.jxm.student.bean.User;
import net.jxm.student.dao.UserDao;
import net.jxm.student.dao.Impl.UserDaoImpl;
import net.jxm.student.service.UserService;
import java.util.List;
/**
* 功能:用户数据访问接口实现类
* 作者:华卫
* 日期:2020年06月06日
*/
public class UserServiceImpl implements UserService {
/**
* 声明用户数据访问对象
*/
private UserDao userDao = new UserDaoImpl();
@Override
public int addUser(User user) {
return userDao.insert(user);
}
@Override
public int deleteUserById(int id) {
return userDao.deleteById(id);
}
@Override
public List<User> findAllUsers() {
return userDao.findAll();
}
@Override
public User findUserById(int id) {
return userDao.findById(id);
}
@Override
public User login(String username, String password) {
return userDao.login(username, password);
}
@Override
public int updateUser(User user) {
return userDao.update(user);
}
@Override
public boolean isUsernameExisted(String username) {
return userDao.isUsernameExisted(username);
}
}
独立完成剩余方法的测试任务
(1)编写测试方法testIsUsernameExisted()
@Test
public void testIsUsernameExisted() {
//定义用户名
String username = "张三丰";
//调用学生数据访问对象的用户存在与否方法
boolean result = service.isUsernameExisted(username);
//判断学生记录是否存在
if (result) {
System.out.println("温馨提示:[" + username + "]已存在,不可用此名注册!");
} else {
System.out.println("温馨提示:[" + username + "]不存在,可用此名注册!");
}
}
(2)编写测试方法testAddUser()
@Test
public void testAddUser() {
//定义用户对象
User user = new User();
//设置用户属性
user.setUsername("王淼");
user.setPassword("999999");
user.setTelephone("15823324590");
user.setRegisterTime(new Date());
//调用用户数据访问对象的插入方法,
int count = service.addUser(user);
//判断用户记录是否插入成功
if (count > 0) {
System.out.println("恭喜,用户记录插入成功!");
System.out.println(service.findUserById(service.findAllUsers().size()));
} else {
System.out.println("遗憾,用户记录插入失败!");
}
}
(3)编写测试方法testDeleteUserById()
@Test
public void testDeleteUserById() {
int id =9;
//调用用户数据访问对象的按id删除方法
int count = service.deleteUserById(id);
//判断是否删除成功
if (count > 0) {
System.out.println("恭喜,用户记录删除成功!");
} else {
System.out.println("遗憾,用户记录删除失败!");
}
}
(4)编写测试方法testUpdateUser()
@Test
public void testUpdateUser() {
//调用用户数据访问对象的查找方法,
User user = service.findUserById(1);
//修改用户信息
user.setUsername("李兰兰");
user.setPassword("452378");
user.setTelephone("18323475829");
//调用用户数据访问对象的更新方法,
int count = service.updateUser(user);
//判断是否更新成功
if (count > 0) {
System.out.println("学校记录更新成功!");
System.out.println(service.findUserById(1));
} else {
System.out.println("学校记录更新失败!");
}
}
(5)编写测试方法testFindUserById()
@Test
public void testFindUserById() {
//调用用户数据访问对象的查找方法
User user = service.findUserById(1);
//输出学校信息
System.out.println("用户名:" + user.getUsername());
System.out.println("密码:" + user.getPassword());
System.out.println("电话:" + user.getTelephone());
System.out.println("注册时间:" + user.getRegisterTime());
}
(6)编写测试方法testFindAllUsers()
@Test
public void testFindAllUsers() {
//调用学生数据访问对象的查找全部方法,
List<User> users = service.findAllUsers();
//通过增强for循环遍历学生列表
for (User user : users) {
System.out.println(user);
}
}