我需要有关单元测试对象的保存和读取方法的最佳方法的帮助。这是一个简单的对象,这里是构造函数:
public Bill(String bname, Double bamount, Date bdate, String bfrequency){
this.billName = bname;
this.billAmount = bamount;
this.billDueDate = bdate;
this.frequency = bfrequency;
}
我实现了一个接口,它将像这样定义一个读取和保存方法
public interface IBill {
public void save(Bill bill) throws IOException;
public Bill read() throws IOException;
请耐心等待,这是上述接口的具体实现
public class BillSvcImpl implements IBill {
@Override
public void save(Bill bill) throws IOException {
FileOutputStream fOut = null;
ObjectOutputStream oOut = null;
try {
fOut = new FileOutputStream("firstbill.ser");
oOut = new ObjectOutputStream(fOut);
oOut.writeObject(bill); //serializing Bill object
} catch (IOException e) {
e.printStackTrace();
try{
oOut.flush();
oOut.close();
fOut.close();
}catch (IOException ioe){
ioe.printStackTrace();
}
}
}
@Override
public Bill read() {
Bill retrievedBill = null;
FileInputStream fIn = null;
ObjectInputStream oIn = null;
try{
fIn = new FileInputStream("firstbill.ser");
oIn = new ObjectInputStream(fIn);
retrievedBill = (Bill)oIn.readObject();
}catch (ClassNotFoundException cnf){
cnf.printStackTrace();
}catch (IOException e){
try{
oIn.close();
fIn.close();
}catch (IOException ioe){
ioe.printStackTrace();
}
}
return retrievedBill;
}
}
最后是我想用来测试这种方法的测试,但它失败了
@Before
public void setUp() throws Exception {
factory = new Factory();
}
@test
public void testSaveBill(){
IBill bill = factory.getBillInfo();
Bill nanny = new Bill("Nanny",128d,new Date(6/28/2013),"Montly");
try {
bill.save(nanny);
} catch (IOException e) {
e.printStackTrace();
}
}
@test
public void testReadBill(){
IBill rbill = factory.getBillInfo();
try {
rbill.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我知道我的测试不正确,所以请帮我重新编写测试。错误信息是Java.lang.Exception: No runnable method