用java测试文件输入流 @覆盖 公共集合getAll(){ try(ObjectInputStream ois=newobjectInputStream(new BufferedInputStream(new FileInputStream(file))){ Object read=ois.readObject(); 列表对象=(ArrayList)已读; 归还物品; }捕获(IOException | ClassNotFoundException ex){ 例如printStackTrace(); 返回新的ArrayList(); } } @试验 公共void testGetAll(){ try(ObjectInputStream ois=newobjectInputStream(new BufferedInputStream(new FileInputStream(“flights.txt”))){ Object read=ois.readObject(); 预期=(ArrayList)读取; }捕获(IOException | ClassNotFoundException ex){ 例如printStackTrace(); } Collection-actual=flightService.getAll(); 资产质量(预期、实际); }

用java测试文件输入流 @覆盖 公共集合getAll(){ try(ObjectInputStream ois=newobjectInputStream(new BufferedInputStream(new FileInputStream(file))){ Object read=ois.readObject(); 列表对象=(ArrayList)已读; 归还物品; }捕获(IOException | ClassNotFoundException ex){ 例如printStackTrace(); 返回新的ArrayList(); } } @试验 公共void testGetAll(){ try(ObjectInputStream ois=newobjectInputStream(new BufferedInputStream(new FileInputStream(“flights.txt”))){ Object read=ois.readObject(); 预期=(ArrayList)读取; }捕获(IOException | ClassNotFoundException ex){ 例如printStackTrace(); } Collection-actual=flightService.getAll(); 资产质量(预期、实际); },java,testing,Java,Testing,嗨,我在测试方面有严重的问题。以上代码是正确的测试方法吗?请帮助我假设您的类被赋予了在构造函数中读取的文件,如下所示: @Override public Collection<Flight> getAll() { try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)))) { Object read = ois.r

嗨,我在测试方面有严重的问题。以上代码是正确的测试方法吗?请帮助我

假设您的类被赋予了在构造函数中读取的文件,如下所示:

@Override
public Collection<Flight> getAll() {
    try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)))) {
        Object read = ois.readObject();
        List<Flight> objects   = (ArrayList<Flight>) read;
        return objects;
    } catch (IOException | ClassNotFoundException ex) {
        ex.printStackTrace();
        return new ArrayList<>();
    }
}

@Test
public void testGetAll() {
    try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("flights.txt")))) {
        Object read = ois.readObject();
        expected = (ArrayList<Flight>) read;
    } catch (IOException | ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    Collection<Flight> actual = flightService.getAll();
    assertEquals(expected, actual);
}
class FlightReader {
    File file;
    public FlightReader(File f) {
        file = f;
    }
    // your getAll here
}
然后,测试将首先使用已知数据创建自己的文件,然后读取该文件,然后验证结果是否符合预期,如下所示:

@Override
public Collection<Flight> getAll() {
    try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)))) {
        Object read = ois.readObject();
        List<Flight> objects   = (ArrayList<Flight>) read;
        return objects;
    } catch (IOException | ClassNotFoundException ex) {
        ex.printStackTrace();
        return new ArrayList<>();
    }
}

@Test
public void testGetAll() {
    try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("flights.txt")))) {
        Object read = ois.readObject();
        expected = (ArrayList<Flight>) read;
    } catch (IOException | ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    Collection<Flight> actual = flightService.getAll();
    assertEquals(expected, actual);
}
class FlightReader {
    File file;
    public FlightReader(File f) {
        file = f;
    }
    // your getAll here
}
@测试
公共void testGetAll(){
航班f1=新航班(“ACREG1”、“B737”);
f2航班=新航班(“ACREG2”、“A320”);
航班f3=新航班(“ACREG3”、“B777”);
List WRITED=新的ArrayList(Arrays.asList(f1、f2、f3));
File tempFile=File.createTempFile(“飞行”、“测试”);
//写入示例数据
try(ObjectOutputStream oos=newobjectoutputstream(newfileoutputstream(tempFile))){
oos.writeObject(书面);
}
//将数据写入文件,然后使用测试代码将其读回
FlightReader=新的FlightReader(临时文件);
List readFlights=reader.getAll();
//验证写入和读取的数据是否相同
包含(f1、f2、f3)的资产;
}
一些注意事项:

  • 您应该尽可能少地使用特定的类,如本例中的
    ArrayList
    。如果最后只返回一个
    集合
    ,为什么要强制转换到
    ArrayList
  • 您根本不应该使用Java序列化;它容易出错,存在安全风险,Java架构师认为这是一个错误

不,这不是测试它的好方法;它复制代码,而不是在结果可预测的地方设置适当的环境,然后进行检查。在
getAll()
方法中,
文件
从哪里来?是的,我觉得是错误的(..getAll()从集合获取的同一位置获取文件..两者都采用文件格式“flights.txt”我的意思是,在测试类中,
file
是如何初始化的。是传递给它的文件路径,还是某个常量值。我在测试类中不初始化任何文件。有一个文件位于project中。我在这两种情况下都读取这些文件(预期和实际).我应该在测试类中初始化新文件吗?我不是指文件本身,而是指
file
变量(包含文件路径)。它是通过构造函数传递给类还是通过某种方法传递给类?这样它就可以创建另一个文件,我们将读取此文件,并检查getall是否包含正确的对象。是的?这里的FlightReader是什么?@SamirAllahverdiyev
FlightReader
是我给你的类的名称,因为你没有提到它。
Flight
class needs提供了一个正确定义的
equals()
方法使其工作。是的,我得到了它