创建服务接口实现类
- 创建学校服务接口实现类
package net.xd.student.service;
import net.xd.student.bean.College;
/**
* 学校服务接口
*/
public interface CollegeService {
College findCollegeById(int id);
int updateCollege(College college);
}
对CollegeServiceImpl进行单元测试
package net.xd.student.test;
import net.xd.student.bean.College;
import net.xd.student.service.CollegeService;
import net.xd.student.service.impl.CollegeServiceImpl;
import org.junit.Test;
/**
* 测试学校服务接口实现类
*/
public class TestCollegeServiceImpl {
//创建学校服务接口对象
CollegeService service =new CollegeServiceImpl();
@Test
public void testFindCollegeById(){
//调用学校服务接口对象的查找方法,获取学校对象
College college=service.findCollegeById(1);
//输出学校信息
System.out.println("校名:"+college.getName());
System.out.println("校长:"+college.getPresident());
System.out.println("地址:"+college.getAddress());
System.out.println("邮箱:"+college.getEmail());
System.out.println("电话:"+college.getTelephone());
}
@Test
public void testUpdateCollege(){
CollegeService service=new CollegeServiceImpl();
College college=service.findCollegeById(1);
college.setPresident("王洪礼");
college.setTelephone("0830-3150380");
int count =service.updateCollege(college);
if (count>0){
System.out.println("更新,学校记录更新成功!");
college=service.findCollegeById(1);
System.out.println(college);
}else{
System.out.println("遗憾,学校记录更新失败!");
}
}
}
- 创建状态服务接口实现类
package net.xd.student.service;
import net.xd.student.bean.Status;
/**
* 状态服务接口
*/
public interface StatusService {
Status findStatusById(int id);
int updateStatus(Status status);
}
对StatusServiceImpl进行单元测试
package net.xd.student.test;
import net.xd.student.bean.Status;
import net.xd.student.service.StatusService;
import net.xd.student.service.impl.StatusServiceImpl;
import org.junit.Test;
/**
* 测试状态服务接口实现类
*/
public class TestStatusServiceImpl {
//创建状态服务接口对象
StatusService service=new StatusServiceImpl();
@Test
public void testFindStatusById(){
//调用状态服务接口对象的查询方法,获取状态对象
Status status=service.findStatusById(1);
//输出状态信息
System.out.println("作者:"+status.getAuthor());
System.out.println("学校:"+status.getCollege());
System.out.println("版本:"+status.getVersion());
System.out.println("地址:"+status.getAddress());
System.out.println("电话:"+status.getTelephone());
System.out.println("邮箱:"+status.getEmail());
}
@Test
public void testUpdateStatus(){
//调用状态服务接口对象的查询方法,获取状态对象
Status status=service.findStatusById(1);
status.setAuthor("金南俊");
status.setTelephone("15800001111");
//调用状态服务接口对象的更新发,更新状态对象
int count =service.updateStatus(status);
if(count>0){
System.out.println("更新,状态记录更新成功!");
status=service.findStatusById(1);
System.out.println(status);
}else{
System.out.println("遗憾,状态记录更新失败!");
}
}
}
- 创建学生服务接口实现类
package net.xd.student.service;
import net.xd.student.bean.Student;
import java.util.List;
import java.util.Vector;
/**
* 学生服务接口
*/
public interface StudentService {
int addStudent (Student student);
int deleteStudentById (String id );
int deleteStudentsByClass(String clazz);
int deleteStudentsByDepartment(String department);
int updateStudent(Student student);
Student findStudentById(String id);
List<Student> findStudentsByName(String name);
List<Student> findStudentsByClass(String clazz);
List<Student> findStudentByDepartment(String department);
List<Student> findAllStudents();
Vector findRowsBySex();
Vector findRowsByClass();
Vector findRowsByDepartment();
}
对StudentServiceImpl进行单元测试
import net.xd.student.bean.Student;
import net.xd.student.service.StudentService;
import net.xd.student.service.impl.StudentServiceImpl;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
/**
* 测试学生服务接口对象
*/
public class TestStudentServiceImpl {
//创建学生服务接口对象
StudentService service=new StudentServiceImpl();
@Test
public void testFindStudentByName(){
String name="李";
//调用学生服务接口对象查询方法,获取学生列表
List<Student> students=service.findStudentsByName(name);
for(Student student: students){
System.out.println(student);
}
}
@Test
public void testAddStudent(){
Student student =new Student();
student.setId("19232302");
student.setName("小邢");
student.setSex("男");
student.setAge(20);
student.setDepartment("艺术传媒学院");
student.setClazz("2019动漫1班");
student.setTelephone("13782470480");
int count =service.addStudent(student);
if(count>0){
System.out.println("学生记录添加成功!");
student=service.findStudentById(student.getId());
System.out.println(student);
}else{
System.out.println("遗憾,学生记录添加失败!");
}
}
@Test
public void testDeleteStudentById(){
String id="19232302";
int count =service.deleteStudentById(id);
if(count>0){
System.out.println("恭喜,学生记录删除成功");
}else{
System.out.println("遗憾,学生记录删除失败!");
}
}
@Test
public void testDeleteStudentsByClass(){
String clazz="2019计应3班";
int count =service.deleteStudentsByClass(clazz);
if(count>0){
System.out.println("恭喜,学生记录删除成功");
}else{
System.out.println("遗憾,学生记录删除失败!");
}
}
@Test
public void testDeleteStudentsByDepartment(){
String department="电子工程学院";
int count =service.deleteStudentsByDepartment(department);
if(count>0){
System.out.println("恭喜,学生记录删除成功");
}else{
System.out.println("遗憾,学生记录删除失败!");
}
}
@Test
public void testUpdateStudent(){
String id ="19232304";
Student student=service.findStudentById(id);
student.setName("小花");
student.setTelephone("15800001156");
//调用状态服务接口对象的更新发,更新状态对象
int count =service.updateStudent(student);
if(count>0){
System.out.println("更新,学生记录更新成功!");
student=service.findStudentById(id);
System.out.println(student);
}else{
System.out.println("遗憾,学生记录更新失败!");
}
}
@Test
public void testFindStudentById(){
String id="19232319";
Student student=service.findStudentById(id);
//输出状态信息
System.out.println("姓名:"+student.getName());
System.out.println("性别:"+student.getSex());
System.out.println("年龄:"+student.getAge());
System.out.println("班级:"+student.getClazz());
System.out.println("系部:"+student.getDepartment());
System.out.println("电话:"+student.getTelephone());
}
@Test
public void testFindAllStudents(){
List<Student> students=service.findAllStudents();
for(Student student: students){
System.out.println(student);
}
}
@Test
public void testFindStudentsByClass(){
String clazz="2019应电3班";
//调用学生服务接口对象查询方法,获取学生列表
List<Student> students=service.findStudentsByClass(clazz);
for(Student student: students){
System.out.println(student);
}
}
@Test
public void testFindStudentsByDepartment(){
String department="艺术传媒学院";
//调用学生服务接口对象查询方法,获取学生列表
List<Student> students=service.findStudentByDepartment(department);
for(Student student: students){
System.out.println(student);
}
}
@Test
public void testFindRowsBySex(){
Vector rows=service.findRowsBySex();
//获取向量的迭代器
Iterator iterator=rows.iterator();
//遍历迭代器
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsByClass(){
Vector rows=service.findRowsByClass();
//获取向量的迭代器
Iterator iterator=rows.iterator();
//遍历迭代器
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsByDepartment(){
Vector rows=service.findRowsByDepartment();
//获取向量的迭代器
Iterator iterator=rows.iterator();
//遍历迭代器
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
- 创建用户服务接口实现类
package net.xd.student.service;
import net.xd.student.bean.User;
import java.util.List;
/**
* 用户服务接口
*/
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);
}
对UserServiceImpl进行单元测试
package net.xd.student.test;
import net.xd.student.bean.User;
import net.xd.student.service.UserService;
import net.xd.student.service.impl.UserServiceImpl;
import org.junit.Test;
import java.util.Date;
import java.util.List;
/**
* 测试用户访问接口实现类
*/
public class TestUserServiceImpl {
//创建用户访问接口对象
UserService service=new UserServiceImpl();
@Test
public void testLogin(){
String username,password;
username="田柾国";
password="111111";
User user=service.login(username,password);
if(user!=null){
System.out.println("恭喜,用户名与密码正确,登录成功!");
}else{
System.out.println("遗憾,用户名与密码错误,登录失败!");
}
}
@Test
public void testIsUsernameExisted(){
//定义用户名
String username="金硕珍";
boolean result=service.isUsernameExisted(username);
if (result){
System.out.println("温馨提示:["+username+"]已存在,不可用此名注册!");
}else{
System.out.println("温馨提示:["+username+"]不存在,可用此名注册!");
}
}
@Test
public void testAddUser(){
User user=new User();
user.setUsername("小花");
user.setPassword("123456");
user.setTelephone("15256789999");
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("遗憾,遗憾记录插入失败!");
}
}
@Test
public void testDeleteUserById(){
int id =1;
//调用学生数据访问对象的按id删除方法
int count =service.deleteUserById(id);
//判断学生记录是否删除成功
if(count>0){
System.out.println("恭喜,学生记录删除成功");
}else{
System.out.println("遗憾,学生记录删除失败!");
}
}
@Test
public void testUpdateUser(){
int id=2;
User user=service.findUserById(id);
user.setUsername("小田");
user.setTelephone("19822423388");
int count =service.updateUser(user);
if (count>0){
System.out.println("用户记录更新成功!");
System.out.println(service.findUserById(id));
}else{
System.out.println("用户记录更新失败!");
}
}
@Test
public void testFindUserById(){
User user =service.findUserById(4);
//输出用户信息
System.out.println("用户名:"+user.getUsername());
System.out.println("密码:"+user.getPassword());
System.out.println("电话:"+user.getTelephone());
System.out.println("注册时间:"+user.getRegisterTime());
}
@Test
public void testFindAllUsers(){
List<User> users=service.findAllUsers();
for(User user: users){
System.out.println(user);
}
}
}