javajunit怎么写( 二 )


this.id = id;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public BigDecimal getPrice() {
return price;
}
public String getIcon() {
return icon;
}
}
//这样的话RUN 的时候就会出现JUnit测试
4. java中如何使用junit呢,求具体步骤 比方说 , 你在A类里面写了一个方法:
pulic void addNumber(int a) {
System.out.println(a+1);
}
这时候 , 你要测试你写的方法对不对 , 就应用到Junit 。通常来说 , 测试类单独写在一个类文件里面 。你可以再创建一个test.java , 在这个类里面你要测试刚才的方法 , 你需要再写一个方法:
在test.java中:
public class test {
@Test (注意这里要导入Junit包 , 按ctrl + 1,Java中 , 任何报错 , 你先按ctrl+1 , 它都会有提示解决方法 。这个1是指Q上面的那个1)
public void testAddNumber() {
A a = new A();
a.addStudent(1);
}
}
最后 , 双击你书写的这个方法 , 就是点testAddNumber点两下 , 然后右键 , run as Junit Test
5. java和junit写类与测试类 /** Book类 */
public class Book {
public String name;
public String author;
private int availNum;
public Book() {
this("", "", 0);
}
public Book(String name, String author, int availNum) {
this.name = name;
this.author = author;
this.availNum = availNum;
}
public int getAvailNum() {
return availNum;
}
public void setAvailNum(int availNum) {
this.availNum = availNum;
}
public boolean subscribe() {
if (availNum > 0) {
availNum--;
return true;
}
return false;
}
public boolean cancelSubscribe() {
availNum++;
return true;
}
}
/** Book类的单元测试类 */
import junit.framework.TestCase;
public class TestBook extends TestCase {
public void testBook() {
Book book = new Book("Thinking in Java", "Bruce Eckel", 3);
assertEquals(true, book.subscribe());// 第一次预借 , 应该是成功的
assertEquals(2, book.getAvailNum());// 此时的可预借数是2
assertEquals(true, book.cancelSubscribe());// 取消预借 , 应该是成功的
assertEquals(3, book.getAvailNum());// 此时的可预借数是3
book.setAvailNum(0);// 将可预借数设为0
assertEquals(false, book.subscribe());// 此时预借 , 应该是不成功的
}
public static void main(String[] args) {
junit.textui.TestRunner.run(Book.class);
}
}
上面的测试类可以不要main函数 , 直接在集成开发环境里用JUnit执行单元测试 。
6. 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() 。