怎样为这样一个类里面的这个方法写JUnit Test Case? 求Test Case的代码.

chongworm 2008-06-04 10:49:35
以下是TestList类的完整代码, 现在想为class TestList中的print方法写JUnit Test Case, 请问Test Case的代码应如何写?


package com.testlist;

import java.util.* ;

interface Book
{
// 书的价格、书的名字、书的信息
public float getPrice() ;
public String getName() ;
public String getInfo() ;
}


class ComputerBook implements Book
{
private String name ;
private float price ;
private String info ;

public ComputerBook(String name,float price,String info)
{
this.setName(name) ;
this.setPrice(price) ;
this.setInfo(info) ;
}

public String getName()
{
return this.name ;
}
public float getPrice()
{
return this.price ;
}
public String getInfo()
{
return this.info ;
}
public void setName(String name)
{
this.name = name ;
}
public void setPrice(float price)
{
this.price = price ;
}
public void setInfo(String info)
{
this.info = info ;
}
public String toString()
{
return "电脑书:书名:"+this.getName()+",价格:"+this.price+",简介:"+this.getInfo() ;
}
};

class BookShop
{
private String name ;
// 一个书店包含多种书
private List allBooks ;

public BookShop()
{
this.allBooks = new ArrayList() ;
}
public BookShop(String name)
{
this() ;
this.setName(name) ;
}

// 得到全部的书
public List getAllBooks()
{
return this.allBooks ;
}

public void append(Book book)
{
this.allBooks.add(book) ;
}

public void setName(String name)
{
this.name = name ;
}
public String getName()
{
return this.name ;
}
};

public class TestList
{
public static void main(String args[])
{
Book b1 = new ComputerBook("JAVA",65.0f,"JAVA 语言的核心技术") ;
Book b2 = new ComputerBook("C++",50.0f,"C++ 语言的核心技术") ;
Book b3 = new ComputerBook("Linux",50.0f,"服务器搭建") ;

BookShop bs = new BookShop("网上书店") ;
bs.append(b1) ;
bs.append(b2) ;
bs.append(b3) ;

print(bs.getAllBooks()) ;
}
//怎样为下面的这个print方法写JUnit Test Case
public static void print(List all)
{
Iterator iter = all.iterator() ;
while(iter.hasNext())
{
Book b = (Book)iter.next() ;
System.out.println(b) ;
}
}
};
...全文
771 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
marf_cn 2008-06-04
  • 打赏
  • 举报
回复
首先,TestList类要继承TestCase
在Junit里是不需要用main去测试的,所有的testcase都写成test*开头的方法,junit会自动执行他。所以你可以写个testPrint方法,new你这个list,然后执行print方法。但因为你的print是void的,所以使用断言没有意义,不过如果出错,junit仍然会帮你找出错误显示red bar

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧