java测试代码

testFindByName():
testFindAll():
testFindRowsBySex():

public class TestStudentDaoImpl {
@Before
public void beforeTest() {
System.out.println(“单元测试开始了!”);
}

@After
public void afterTest() {
    System.out.println("单元测试结束了!");
}

@Test
public void testInsert() {
    Student student = new Student();
    student.setId("18205071");
    student.setName("王亮琴");
    student.setSex("女");
    student.setAge(19);
    student.setDepartment("信息工程系");
    student.setTelephone("1878798984");

    StudentDao dao = new StudentDaoImpl();
    int count = dao.insert(student);
    if (count > 0) {
        System.out.println("恭喜,学生记录插入成功!");
    }else {
        System.out.println("遗憾,学生记录插入失败!");
    }
}

@Test
public void testDeleteById() {
    StudentDao dao = new StudentDaoImpl();
    String id = "18205113";
    int count = dao.deleteById(id);
    if (count > 0) {
        System.out.println("恭喜,学生记录删除成功!");
    }else {
        System.out.println("遗憾,学生记录删除失败!");
    }
}


@Test
public void testDeleteByClass() {
    StudentDao dao = new StudentDaoImpl();
    String clazz = "15英教1班";
    int count = dao.deleteByClass(clazz);
    if (count > 0) {
        System.out.println("恭喜,["+ clazz +"]学生记录删除成功");
    }else {
        System.out.println("遗憾,[" + clazz +"]学生记录删除失败");
    }
}

@Test
public void testDeleteByDepartment() {
    StudentDao dao = new StudentDaoImpl();
    String department = "建筑系";
    int count = dao.deleteByDepartment(department);
    if (count > 0) {
        System.out.println("恭喜,[" + department +"]记录删除成功");
    }else {
        System.out.println("遗憾,[" + department +"]记录删除失败");
    }

}

@Test
public void testUpdate() {
    Student student = new Student();
    student.setName("王狗屎");
    student.setId("15012456");
    StudentDao dao = new StudentDaoImpl();
    int count = dao.update(student);
    if (count > 0) {
        System.out.println("恭喜,记录更新成功");
    }else {
        System.out.println("遗憾,记录更新失败");
    }

}

@Test
public void testFindById() {

    StudentDao dao = new StudentDaoImpl();
    String id = "16080305";
    Student student = dao.findById(id);
    System.out.println(student);

}

@Test
public void testFindByClass() {
    StudentDao dao = new StudentDaoImpl();
    String clazz = "16英教1班";
    List students = dao.findByClass(clazz);
    for (Student student: students) {
        System.out.println(student);
    }
}

@Test
public void testFindByDepartment() {
    StudentDao dao = new StudentDaoImpl();
    String department = "信息工程系";
    List students = dao.findByDepartment(department);
    for (Student student:students) {
        System.out.println(student);
    }
}

@Test
public void testFindAll() {
    StudentDao dao = new StudentDaoImpl();
    List students = dao.findAll();
    for (Student student:students) {
        System.out.println(student);
    }
}



@Test
public void testFindByName() {
    StudentDao dao = new StudentDaoImpl();
    String name = "哼";
    List students = dao.findByName(name);
    if (students.size() > 0) {
        for (Student student : students) {
            System.out.println(student);
        }
    }else {
        System.out.println("温馨提示:查无此人!"); }
}

@Test
public void testFindRowsBySex() {
    StudentDao dao = new StudentDaoImpl();
    Vector rows = dao.findRowsBySex();
    Iterator iterator = rows.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}

@Test
public void testFindRowsByClass() {
    StudentDao dao = new StudentDaoImpl();
    Vector rows = dao.findRowsByClass();
    Iterator iterator = rows.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}

}

添加测试方法testIsUsernameExisted()

package net.wlq.student.bean.test;

import net.wlq.student.bean.bean.User;
import net.wlq.student.bean.dao.UserDao;
import net.wlq.student.bean.dao.impl.UserDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.sql.Timestamp;
import java.util.List;

/**

  • 测试用户数据访问接口实现类
    */

public class TestUserDaoImpl {

@Before
public void beforeTest() {
    System.out.println("单元测试开始了!");
}

@After
public void afterTest() {
    System.out.println("单元测试结束了!");
}

@Test
public void testInsert() {
    User user = new User();
    user.setUsername("AAA");
    user.setPassword("456");
    user.setTelephone("1564658625");
    user.setRegisterTime(new Timestamp(2019-1-1));
    UserDao dao = new UserDaoImpl();
    int count = dao.insert(user);
    if (count > 0) {
        System.out.println("恭喜,学生记录插入成功");
    }else {
        System.out.println("遗憾,学生记录插入失败");
    }

}

@Test
public void testDeleteById() {
    UserDao dao = new UserDaoImpl();
    int count = dao.deleteById(1);
    if (count > 0) {
        System.out.println("恭喜,删除记录成功");
    }else {
        System.out.println("遗憾,删除记录失败");
    }
}

@Test
public void testUpdate() {
    User user = new User();

    user.setUsername("王aa");
    user.setPassword("456123");

// user.setRegisterTime(new Timestamp(new Date().getTime()));
user.setRegisterTime(new Timestamp(System.currentTimeMillis()));
user.setId(5);
UserDao dao = new UserDaoImpl();
int count = dao.update(user);
if (count > 0) {
System.out.println(“恭喜,更新记录成功”);
}else {
System.out.println(“遗憾,更新记录失败”);
}

}

@Test
public void testFindById() {
    UserDao dao = new UserDaoImpl();
    User user = dao.findById(1);
    System.out.println(user);
}

@Test
public void testFindAll() {
    UserDao dao = new UserDaoImpl();
    List users = dao.findAll();
    for (User user:users) {
        System.out.println(user);
    }
}

@Test
public void  testLogin() {
    UserDao dao = new UserDaoImpl();
    String username, password;
    username = "admin";
    password = "12345";

    User user = dao.login(username, password);
    if(user != null) {
        System.out.println("恭喜,用户名与密码正确,登陆成功!");
    }else {
        System.out.println("遗憾,用户名与密码错误,登陆失败!");
    }
}

@Test
public void testLogin1() {
    UserDao dao = new UserDaoImpl();
    String username, password;
    username = "howord";
    password = "123456";
    User user = dao.login(username, password);
    if (user != null) {
        System.out.println("恭喜,用户名与密码正确,登陆成功!");
    }else {
        System.out.println("遗憾,用户名与密码错误,登陆失败!");
    }
}

@Test
public void testFindUsername() {
    UserDao dao = new UserDaoImpl();
    String username = "admin";
    User user1 = dao.findUsername(username);
    if (user1 != null) {
        System.out.println("遗憾,["    + username +"]已存在,请重新输入用户名!");
    }else {
        System.out.println("恭喜不存在,,["    + username +"] ,可以注册!");
    }

}

}

编写测试方法testLogin()方法:

package net.wlq.student.bean.test;

import net.wlq.student.bean.bean.Student;
import net.wlq.student.bean.service.StudentService;
import net.wlq.student.bean.service.impl.StudentServiceImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

/**

  • 功能:测试学生服务接口实现类
    */

public class TestStudentServiceImpl {
@Before
public void beforeTest() {
System.out.println(“单元测试开始了!”);
}

@After
public void afterTest() {
    System.out.println("单元测试结束了!");
}

// @Test
// public void testFindStudentsByName() {
// StudentService service = new StudentServiceImpl();
// String name = “李”;
// List students = service.findStudentsByName(name);
// for (Student student: students) {
// System.out.println(student);
// }
// }

@Test
public void testAddStudent() {
    StudentService service = new StudentServiceImpl();
    Student student = new Student();

    student.setName("张发");
    student.setId("10235623");
    student.setTelephone("156324835");
    student.setClazz("软件1班");
    student.setSex("男");
    student.setAge(20);

    int count = service.addStudent(student);
    if (count > 0) {
        System.out.println("恭喜,学生数据插入成功");
        System.out.println(student);
    }else {
        System.out.println("遗憾,学生数据插入失败");
    }
}

@Test
public void testDeleteStudentId() {
    StudentService service = new StudentServiceImpl();
    String id = "10235623";
    int count = service.deleteStudentById(id);
    if (count > 0) {
        System.out.println("恭喜,["+ id + "]学生记录删除成功");
    }else {
        System.out.println("遗憾,[\"+ id + \"]学生记录删除失败");
    }
}

@Test
 public void testDeleteStudentClass() {
    StudentService service = new StudentServiceImpl();
    String clazz = "11软件2班";
    int count = service.deleteStudentsByClass(clazz);
    if (count > 0) {
        System.out.println("恭喜,["+ clazz +"]学生记录删除成功");
    }else {
        System.out.println("遗憾,[\"+ clazz +\"]学生记录删除失败");
    }
}
@Test
public void testDeleteStudentsDepartment() {
    StudentService service = new StudentServiceImpl();
    String department = "信息工程系";
    int count  = service.deleteStudentsByDepartment(department);
    if (count > 0) {
        System.out.println("恭喜,[" + department +"]学生记录删除成功");
    }else {
        System.out.println("遗憾,[" + department +"]学生记录删除失败");
    }
}

@Test
public void testUpdateStudent() {
    StudentService service = new StudentServiceImpl();
    String id = "16080304";
    Student student = service.findStudentById(id);
    student.setName("琉厂");
    student.setTelephone("15632483551");
    int count = service.updateStudent(student);
    if (count > 0) {
        System.out.println("恭喜,数据更新成功");
        student = service.findStudentById("16080304");
        System.out.println(student);
    }else {
        System.out.println("遗憾,数据更新失败");
    }
}

@Test
public void testFindStudentById() {
    StudentService service = new StudentServiceImpl();
    Student student = service.findStudentById("16090703");
    System.out.println(student);
}

@Test
public void testFindStudentsByName() {
    StudentService service = new StudentServiceImpl();
    String name = "华晨";
    List students = service.findStudentsByName(name);
    if (students.size() > 0) {
        for (Student student:students) {
            System.out.println(student);
        }
    }else {
        System.out.println("温馨提示:查无此人");
    }
}

@Test
public void testFindStudentsByClass() {
    StudentService service = new StudentServiceImpl();
    String clazz = "16英教1班";
    List students = service.findStudentsByClass(clazz);
    if (students.size() > 0) {
        for (Student student:students) {
            System.out.println(student);
        }
    }else {
        System.out.println("温馨提示:没有该班级");
    }

}

@Test
public void testFindStudentsByDepartment() {
    StudentService service = new StudentServiceImpl();
    String department = "外语系";
    List students = service.findStudentsByDepartment(department);
    if (students.size() > 0) {
        for (Student student:students) {
            System.out.println(student);
        }
    }else {
        System.out.println("温馨提示:没有该系学生信息");
    }
}

@Test
public void testFindAllStudents() {
    StudentService service = new StudentServiceImpl();
    List students = service.findAllStudents();
    for (Student student:students) {
        System.out.println(student);
    }
}

@Test
public void testFindRowsBySex() {
    StudentService service = new StudentServiceImpl();
    Vector rows = service.findRowsBySex();
    Iterator iterator = rows.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}

@Test
public void testFindRowsByClass() {
    StudentService service = new StudentServiceImpl();
    Vector rows = service.findRowsByClass();
    Iterator iterator = rows.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}

@Test
public void testFindRowsByDepartment() {
    StudentService service = new StudentServiceImpl();
    Vector rows = service.findRowsByDepartment();
    Iterator iterator = rows.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}

}

你可能感兴趣的:(java上课笔记)