下面是我做項目時的例子,希望對你有所幫助。
正定網站建設公司創(chuàng)新互聯,正定網站設計制作,有大型網站制作公司豐富經驗。已為正定數千家提供企業(yè)網站建設服務。企業(yè)網站搭建\成都外貿網站制作要多少錢,請找那個售后服務好的正定做網站的公司定做!
/*
*@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","信發(fā)號","張建","某出版社","電信",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 allLibManager.size();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 allStudent.size();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;
}
}
在Eclipse中查看JDK類庫的源代碼
設置:
1.點 “window”- "Preferences" - "Java" - "Installed JRES"
2.此時"Installed JRES"右邊是列表窗格,列出了系統(tǒng)中的 JRE 環(huán)境,選擇你的JRE,然后點邊上的 "Edit...", 會出現一個窗口(Edit JRE)
3.選中rt.jar文件的這一項:“c:\program files\java\jre_1.5.0_06\lib\rt.jar”?
點 左邊的“+” 號展開它
4.展開后,可以看到“Source Attachment:(none)”,點這一項,點右邊的按鈕“Source Attachment...”, 選擇你的JDK目錄下的 “src.zip”文件
5.一路點"ok",結束。
dt.jar是關于運行環(huán)境的類庫,主要是swing的包?
tools.jar是關于一些工具的類庫?
rt.jar包含了jdk的基礎類庫,也就是你在java doc里面看到的所有的類的class文件
使用:
可以在 Java 源代碼編輯器或代碼片段編輯測試窗中選擇類型、方法或字段的名稱,然后對元素的定義打開編輯器。
在 Java 編輯器中,選擇類型、方法或字段的名稱。您也可以僅僅在名稱中單擊一次。?
執(zhí)行下列其中一項操作:?
1.從菜單欄中,選擇瀏覽 打開聲明?
2.從編輯器的彈出菜單中,選擇打開聲明?
3.按 F3 鍵,如下圖
上面 wuzhikun12同學寫的不錯,但我想還不能運行,并且還不太完善。我給個能運行的:(注意:文件名為:Test.java)
//要實現對象間的比較,就必須實現Comparable接口,它里面有個compareTo方法
//Comparable最好使用泛型,這樣,無論是速度還是代碼量都會減少
@SuppressWarnings("unchecked")
class Student implements ComparableStudent{
private String studentNo; //學號
private String studentName; //姓名
private double englishScore; //英語成績
private double computerScore; //計算機成績
private double mathScore; //數學成績
private double totalScore; //總成績
//空構造函數
public Student() {}
//構造函數
public Student(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
this.studentNo = studentNo;
this.studentName = studentName;
this.englishScore = englishSocre;
this.computerScore = computerScore;
this.mathScore = mathScore;
}
//計算總成績
public double sum() {
this.totalScore = englishScore+computerScore+mathScore;
return totalScore;
}
//計算評測成績
public double testScore() {
return sum()/3;
}
//實現compareTO方法
@Override
public int compareTo(Student student) {
double studentTotal = student.getTotalScore();
return totalScore==studentTotal?0:(totalScorestudentTotal?1:-1);
}
//重寫toString方法
public String toString(){
return "學號:"+this.getStudentNo()+" 姓名:"+this.getStudentName()+" 英語成績:"+this.getEnglishScore()+" 數學成績:"+this.getMathScore()+" 計算機成績:"+this.getComputerScore()+" 總成績:"+this.getTotalScore();
}
//重寫equals方法
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(!(obj instanceof Student)){
return false;
}
Student student = (Student)obj;
if(this.studentNo.equals(student.getStudentName())) { //照現實來說,比較是不是同一個學生,應該只是看他的學號是不是相同
return true;
} else {
return false;
}
}
/*以下為get和set方法,我個人認為,totalScore的set的方法沒必要要,因為它是由其它成績計算出來的
在set方法中,沒設置一次值,調用一次sum方法,即重新計算總成績
*/
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
sum();
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
sum();
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
sum();
}
public double getComputerScore() {
return computerScore;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
sum();
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
sum();
}
public double getTotalScore() {
return totalScore;
}
}
//Student子類學習委員類的實現
class StudentXW extends Student {
//重寫父類Student的testScore()方法
@Override
public double testScore() {
return sum()/3+3;
}
public StudentXW() {}
//StudentXW的構造函數
public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//Student子類班長類的實現
class StudentBZ extends Student {
//重寫父類Student的testScore()方法
@Override
public double testScore() {
return sum()/3+5;
}
public StudentBZ() {}
//StudentXW的構造函數
public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//測試類
public class Test {
public static void main(String[] args) {
//生成若干個student類、StudentXW類、StudentBZ類
Student student1 = new Student("s001","張三",70.5,50,88.5);
Student student2 = new Student("s002","李四",88,65,88.5);
Student student3 = new Student("s003","王五",67,77,90);
StudentXW student4 = new StudentXW("s004","李六",99,88,99.5);
StudentBZ student5 = new StudentBZ("s005","朱漆",56,65.6,43.5);
Student[] students = {student1,student2,student3,student4,student5};
for(int i = 0 ; istudents.length; i++){
double avgScore = students[i].testScore();
System.out.println(students[i].getStudentName()+"學生的評測成績?yōu)椋?+ avgScore+"分");
}
}
}
運行結果為:
張三學生的評測成績?yōu)椋?9.66666666666667分
李四學生的評測成績?yōu)椋?0.5分
王五學生的評測成績?yōu)椋?8.0分
李六學生的評測成績?yōu)椋?8.5分
朱漆學生的評測成績?yōu)椋?0.03333333333333分
分享標題:java測試類源代碼 java測試項目
分享URL:http://jinyejixie.com/article46/doscihg.html
成都網站建設公司_創(chuàng)新互聯,為您提供做網站、網站策劃、Google、建站公司、網站收錄、營銷型網站建設
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯