0

我需要有关单元测试对象的保存和读取方法的最佳方法的帮助。这是一个简单的对象,这里是构造函数:

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

4

1 回答 1

0

如果您使用 JUnit 4.x,那么您需要使用@Test3.x 测试类的注释(大小写问题)标记您的方法,extends TestCase以使您的测试正常工作。

如果您设计单元测试,则不必实际序列化您的对象然后反序列化它。我会使用方法移动FileInputStream fInObjectInputStream oIn类级别属性get/set并在测试中模拟它们(很少有好的模拟框架实现12

于 2013-07-28T22:01:50.913 回答