资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java测试类源代码 java测试项目

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

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

正定网站建设公司创新互联,正定网站设计制作,有大型网站制作公司丰富经验。已为正定数千家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的正定做网站的公司定做!

/*

*@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 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怎么查看java源代码

在Eclipse中查看JDK类库的源代码

设置:

1.点 “window”- "Preferences" - "Java" - "Installed JRES"

2.此时"Installed JRES"右边是列表窗格,列出了系统中的 JRE 环境,选择你的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是关于运行环境的类库,主要是swing的包 

tools.jar是关于一些工具的类库 

rt.jar包含了jdk的基础类库,也就是你在java doc里面看到的所有的类的class文件

使用:

可以在 Java 源代码编辑器或代码片段编辑测试窗中选择类型、方法或字段的名称,然后对元素的定义打开编辑器。

在 Java 编辑器中,选择类型、方法或字段的名称。您也可以仅仅在名称中单击一次。 

执行下列其中一项操作: 

1.从菜单栏中,选择浏览 打开声明 

2.从编辑器的弹出菜单中,选择打开声明 

3.按 F3 键,如下图

高分求两个简单的JAVA设计源代码

上面 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()+"学生的评测成绩为:"+ avgScore+"分");

}

}

}

运行结果为:

张三学生的评测成绩为:69.66666666666667分

李四学生的评测成绩为:80.5分

王五学生的评测成绩为:78.0分

李六学生的评测成绩为:98.5分

朱漆学生的评测成绩为:60.03333333333333分


本文标题:java测试类源代码 java测试项目
标题URL:http://cdkjz.cn/article/doscihg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220