求助含有Junit测试代码的Java源代码,用来学习Junit

希望各位给与帮助,如果比较好,我会另有加分
2024-11-20 04:37:47
推荐回答(1个)
回答(1):

下面是我做项目时的例子,希望对你有所帮助。

/*
*@author ougaoyan ,date:2008-10-19
*/
package test;

import java.util.Date;

import junit.framework.TestCase;
import app.DA.BookDA;
import app.PD.Book;

public class TestBookDA extends TestCase {

public TestBookDA(String name){
super(name);
}
// public Book(int bookID, String cip, String name, String author,String press, String category, int quantity, int reborrowable,int borrowerID,Date startDate)
public void testEditBook(){
Book book1 = new Book(1,"123456","信号","张建","某出版社","电信",1,1,1,new Date());
Book book2 = new Book(-1,"123456","信号","张建","某出版社","电信",1,1,1,new Date());
Book book3 = new Book(99,"123456","信号","张建","某出版社","电信",1,1,1,new Date());
Book book4 = new Book(1,"123456","信号","张建","某出版社","电信",1,1,1,new Date());
assertEquals(true,BookDA.editBook(book1));
assertEquals(false,BookDA.editBook(book2));
assertEquals(true,BookDA.editBook(book3));
assertEquals(false,BookDA.editBook(book4));
}
////Book(int borrowerID, Date startDate, int reBorrowable, String cip)
public void testAddBook(){
Book book1 = new Book(0,"234567","信发号","张建","某出版社","电信",1,1,1,new Date());
Book book2 = new Book(0,"123456","信的号","张建","某出版社","电信",1,1,1,new Date());
Book book3 = new Book(0,"99999","信i号","张建","某出版社","电信",1,1,1,new Date());
assertEquals(true,BookDA.addBook(book1));
assertEquals(true,BookDA.addBook(book2));
assertEquals(false,BookDA.addBook(book3));
}

public static void main(String[] args) {

junit.textui.TestRunner.run(TestBookDA.class);
System.out.println(new TestBookDA("TestBookDA").countTestCases());

}
}

/*
*@author ougaoyan ,date:2008-10-19
*/
package test;

import java.util.Date;
import java.util.Vector;
import junit.framework.TestCase;
import app.DA.CipDA;
import app.PD.Cip;

public class TestCipDA extends TestCase {
public TestCipDA (String name){
super(name);
}

public void testFindBooksByName(){
String name1 = "数据库";
String name2 = "小小";
Vector vector1 = CipDA.findBooksByName(name1);
Vector vector2 = CipDA.findBooksByName(name2);
assertNotNull(vector1);
assertNull(vector2);

}
public void testFindBooksByAuthor(){
String name1 = "欧阳";
String name2 = "小小";
Vector vector1 = CipDA.findBooksByAuthor(name1);
Vector vector2 = CipDA.findBooksByAuthor(name2);
assertNotNull(vector1);
assertNull(vector2);

}
public void testFindBooksByCategory(){
String name1 = "计算机";
String name2 = "计 算 机";
String name3 = "wucimin";
Vector vector1 = CipDA.findBooksByCategory(name1);
Vector vector2 = CipDA.findBooksByCategory(name2);
Vector vector3 = CipDA.findBooksByCategory(name3);
assertNotNull(vector1);
assertNotNull(vector2);
assertNull(vector3);
}
// public void testEditCip(){

// }
//public Cip(String cip, String name, String author, String press,String category, int quantity, int reserverID,Date reservedDate)
public void testAddCip(){
Cip cip1 = new Cip("2244","新加书","新者","出版社","计算机",3,0,new Date());
//Cip cip2 =new Cip(null,"新加书","新者","出版社","计算机",3,0,new Date());
Cip cip3 =new Cip("22er3rr3rt4t43t43t3t34t34t34t44","新加书","新者","出版社","计算机",3,0,new Date());
assertEquals(true,CipDA.addCip(cip1));
//assertEquals(false,CipDA.addCip(cip2));
assertEquals(false,CipDA.addCip(cip3));
}
public void testEditCip(){
Cip cip1 = new Cip("2244","新加书","新者","出版社","计算机",3,0,new Date());
Cip cip2 =new Cip(null,"新加书","新者","出版社","计算机",3,0,new Date());
Cip cip3 =new Cip("22er3rr3rt4t43t43t3t34t34t34t44","新加书","新者","出版社","计算机",3,0,new Date());
assertEquals(true,CipDA.editCip(cip1));
assertEquals(false,CipDA.editCip(cip2));
assertEquals(false,CipDA.editCip(cip3));
}

public static void main(String[] args) {

junit.textui.TestRunner.run(TestCipDA.class);
System.out.println(new TestCipDA("TestCipDA").countTestCases());

}
}

/*
*@author ougaoyan ,date:2008-10-19
*/
package test;
import java.util.Date;
import java.util.Vector;

import junit.framework.TestCase;
import app.DA.LibManagerDA;
import app.PD.LibManager;
import app.PD.Student;

public class TestLibManagerDA extends TestCase {

public TestLibManagerDA(String name){
super(name);
}
public void testCheck(){
String userName1 = "1234";
String password1 = "111111";
String userName2 = "";
String password2= "";
String userName3 = "1234";
String password3 = "111";
LibManager libmanager = new LibManager(1,userName1,password1,"w");
assertEquals(libmanager.getID(), LibManagerDA.check(userName1,password1).getID());
assertEquals(libmanager.getName(), LibManagerDA.check(userName1,password1).getName());
assertEquals(null, LibManagerDA.check(userName2,password2));
assertEquals(null, LibManagerDA.check(userName3,password3));
}

public void testUpdatePwd(){
int id1 =1,id2=99;
String qpwd="234567";
assertEquals(true,LibManagerDA.updatePwd(id1, qpwd));
assertEquals(false,LibManagerDA.updatePwd(id2, qpwd));
}
public void testSearchLibManager(){
LibManager libmanager1 = new LibManager(1,"1234","111111","w");
LibManager libmanager2 = new LibManager(2,"2345","23456","x");
assertEquals(libmanager1.getID(), LibManagerDA.searchLibManager("1234").getID());
assertEquals(libmanager2.getID(), LibManagerDA.searchLibManager("2345").getID());
assertNull(LibManagerDA.searchLibManager(""));
}
public void testQuantity(){
String cip1 = "123456";
String cip2 = "234567";
assertEquals(1,LibManagerDA.getBookQuantity(cip1));
assertEquals(3,LibManagerDA.getBookQuantity(cip2));
assertEquals(-1,LibManagerDA.getBookQuantity(""));
}
public void testEditLibManager(){
LibManager libmanager1 = new LibManager(1,"1234","111111","w");
LibManager libmanager2 = new LibManager(2,"2345","23456","x");
//LibManager libmanager3 = new LibManager(90,"123456677","0","y");
LibManager libmanager4 = new LibManager(3,"123456677","0","e");
assertEquals(true, LibManagerDA.editLibManager(libmanager1));
assertEquals(true,LibManagerDA.editLibManager(libmanager2));
//assertEquals(false,LibManagerDA.editLibManager(libmanager3));
assertEquals(false,LibManagerDA.editLibManager(libmanager4));
}
public void testAddLibManager(){
LibManager libmanager1 = new LibManager(-1,"1234","0","z");
LibManager libmanager2 = new LibManager(-1,"2345","0","x");
LibManager libmanager3 = new LibManager(-1,"12hh3456677","0","y");
assertEquals(true, LibManagerDA.addLibManager(libmanager1));
assertEquals(true,LibManagerDA.addLibManager(libmanager2));
assertEquals(false,LibManagerDA.addLibManager(libmanager3));
}
public void testGetBookCip(){
int bookid1 =1;
int bookid2 =4;
int bookid3 = -1;
String exceptedcip1 = "123456";
String exceptedcip2="234567";
String exceptedcip3 = null;
assertEquals(exceptedcip1, LibManagerDA.getBookCip(bookid1));
assertEquals(exceptedcip2, LibManagerDA.getBookCip(bookid2));
assertEquals(exceptedcip3, LibManagerDA.getBookCip(bookid3));

}
public void testCheckReserved(){
String cip1 = "123456";
String cip2 = "234567";
int studentid1 = 1;
int studentid2 = 2;
int studentid3 = 99;
assertEquals(true, LibManagerDA.checkReserved(cip1,studentid1));
assertEquals(true, LibManagerDA.checkReserved(cip2,studentid2));
assertEquals(false, LibManagerDA.checkReserved(cip1,studentid2));
assertEquals(false,LibManagerDA.checkReserved(cip2,studentid1));
assertEquals(false,LibManagerDA.checkReserved(cip2,studentid3));
assertEquals(false,LibManagerDA.checkReserved(cip1,studentid3));
}

//public static boolean borrowOPeration(int quantity, int bookID, int borrowerID,int reservedNum, int borrowedNum,Date startDate)
public void testBorrowOPeration(){
int quantity1 = 1;
int bookID1 = 2;
int borrowerID1 = 3;
int reservedNum1 = 1;
int borrowedNum1 = 4;
int quantity2 = 1;
int bookID2 = 1;
int borrowerID2 = 3;
int reservedNum2 = 0;
int borrowedNum2 = 7;
assertEquals(true,LibManagerDA.borrowOperation(quantity1, bookID1, borrowerID1, reservedNum1, borrowedNum1, new Date()));
assertEquals(false,LibManagerDA.borrowOperation(quantity2, bookID2, borrowerID2, reservedNum2, borrowedNum2, new Date()));
}
//public static boolean returnOperation (int quantity, int bookID, int borrowerID, int borrowedNum, Date startDate, int reborrowable)
public void testReturnOperation(){
int quantity1 = 1;
int bookID1 = 1;
int borrowerID1 = 1;
int borrowedNum1 = 4;
int reborrowable1 = 0;
int quantity2 = 1;
int bookID2 = 2;
int borrowerID2 = 2;
int borrowedNum2 = 7;
int reborrowable2 = 0;
assertEquals(true,LibManagerDA.returnOperation(quantity1, bookID1, borrowerID1, borrowedNum1, new Date(),reborrowable1));
assertEquals(true,LibManagerDA.returnOperation(quantity2, bookID2, borrowerID2, borrowedNum2,new Date(), reborrowable2));
}
public void testGetBookQuantity(){
String cip1 = "123456";
String cip2 = "234567";
int quantity1 = 5;
int quantity2 = 3;
assertEquals(quantity1,LibManagerDA.getBookQuantity(cip1));
assertEquals(quantity2,LibManagerDA.getBookQuantity(cip2));

}
public void testFindBill(){
String userName1 = "111111";
String userName2 = "222222";
float bill1 = 0;
float bill2 = 0;
assertEquals(bill1,LibManagerDA.findBill(userName1),0.000001);
assertEquals(bill2,LibManagerDA.findBill(userName2),0.000001);
}
public void testClearBill(){
String userName1 = "111111";
String userName2 = "222222";
assertEquals(true,LibManagerDA.clearBill(userName1));
assertEquals(true,LibManagerDA.clearBill(userName2));
}
public void testSelectAllLibManager(){
Vector allLibManager = new Vector();
allLibManager = LibManagerDA.selectAllLibManager();
for(int i = 0;i LibManager libManager = (LibManager)allLibManager.get(i);
System.out.println(libManager.getName());
}
}
public void testSecelctAllStudent(){
Vector allStudent = new Vector();
allStudent = LibManagerDA.secelctAllStudent();
for(int i = 0;i Student student = (Student)allStudent.get(i);
System.out.println(student.getName());
}
}
public void testStartTimes(){
Vector times = new Vector();
times = LibManagerDA.startTimes(0);
for(int i = 0;i < times.size();i ++)
System.out.println((Date)times.get(i));
}
public void testUpdateStuBill() {

}

public static void main(String[] args) {

junit.textui.TestRunner.run(TestLibManagerDA.class);
System.out.println(new TestLibManagerDA("dd ").countTestCases());

}

}

/*
*@author ougaoyan ,date:2008-10-19
*/
package test;

import java.util.Date;
import java.util.Vector;

import junit.framework.TestCase;
import app.DA.StudentDA;
import app.PD.Book;
import app.PD.Cip;
import app.PD.Student;

public class TestStudentDA extends TestCase {

public TestStudentDA(String name){
super(name);
}
/*public Student(int id, int borrowedNum, int reservedNum, float bill,
String userName, String password, String name, String department,
String unit, String sex) */
public void testCheck(){
Student student1 = new Student(1,0,0,0,"111111","123456","张三","软件","0602","男");
//Student student2 = new Student(1,0,0,0,"111111","123","张三","软件","0602","男");
//Student student3 = new Student(2,0,0,0,"11111","123456","张三","软件","0602","男");
assertEquals(student1.getId(),StudentDA.check("111111","123456").getId());
assertEquals(true,student1.getId()==StudentDA.check("111111","123456").getId());
assertEquals(null,StudentDA.check("111111","12356"));

//assertEquals(false,);
//assertEquals(false,);
}
public void testUpdatePwd(){
int id1 = 1;
int id2 = 2;
int id3 = -1;
String password1 = "123456";
String password2 = "234567";
String password3 = "1234555";
assertEquals(true,StudentDA.updatePwd(id1, password1));
assertEquals(true,StudentDA.updatePwd(id2, password2));
assertEquals(false,StudentDA.updatePwd(id3, password3));

}
public void testSearchStudent(){
Student student1 = new Student(1,0,0,0,"111111","123456","张三","软件","0602","男");
assertEquals(true,student1.getId()==StudentDA.searchStudent("111111").getId());
assertEquals(null,StudentDA.searchStudent("111"));
}
public void testEditStudent(){
Student student1 = new Student(1,0,0,0,"111111","123456","张三","软件","0602","女");
Student student2 = new Student(1,0,0,0,"11111333555555555556631","123456","张三","软件","0602","女");
assertEquals(true,StudentDA.editStudent(student1));
assertEquals(false,StudentDA.editStudent(student2));
}
public void testAddStudent(){
Student student1 = new Student(5,0,0,0,"111111","123456","张三","软件","0602","女");
Student student2 = new Student(1,0,0,0,"1111133331","123456","张三","软件","0602","女");
assertEquals(true,StudentDA.addStudent(student1));
//assertEquals(false,StudentDA.addStudent(student2));
}
public static void testGetBorrowBookInfor(){
int studentid1 = 1;
int studentid2 = 2;
int studentid3 = 5;
Vector vector1 = new Vector();
Vector vector2 = new Vector();
Vector vector3 = new Vector();
vector1 = StudentDA.getBorrowBookInfor(studentid1);
vector2 = StudentDA.getBorrowBookInfor(studentid2);
vector3 = StudentDA.getBorrowBookInfor(studentid3);
//System.out.println(vector1);
assertNotNull(((Book)vector1.get(0)).getName());

}
public void testGetReserveBookInfor(){
int studentid1 = 1;
Vector vector1 = new Vector();
vector1 = StudentDA.getReserveBookInfor(studentid1);
assertEquals("数据库",((Cip)vector1.get(0)).getCategory());
}
//public static boolean reserveOperation(int reserverID1, String cip1, int quantity1, int reservedNum1, Date reservedDate1 )
public void testReserveOperation(){
assertEquals(true,StudentDA.reserveOperation(1, "123456", 1, 2, new Date()));
}
//void cancelReservation(int reserverID1,String cip1, int quantity1, int reserveNum1)
public void testCancelReservation(){
assertTrue(StudentDA.cancelReservation(1, "1234567", 1, 3));
}
//public static boolean updateReborrowable(int bookID, int reborrowable1, int studentID,Date startDate1)
public void testUpdateReborrowable(){
assertTrue(StudentDA.updateReborrowable(2,0,1,new Date()));
}
public static void main(String[] args) {

//junit.textui.TestRunner.run(TestStudentDA.class);
//System.out.println(new TestStudentDA("TestStudentDA").countTestCases());
testGetBorrowBookInfor();

}

}

/*
*@author ougaoyan ,date:2008-10-19
*/
package test;
import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTests {

public static Test suite() {
TestSuite suite = new TestSuite("Test for test");
suite.addTestSuite(TestLibManagerDA.class);
suite.addTestSuite(TestBookDA.class);
suite.addTestSuite(TestCipDA.class);
suite.addTestSuite(TestStudentDA.class);
//suite.addTestSuite(TestLibManagerDA.class);
//$JUnit-END$
return suite;
}

}