如何编写涉及javafx文件选择器的序列化和反序列化测试?

如何编写涉及javafx文件选择器的序列化和反序列化测试?,java,javafx,Java,Javafx,我需要为序列化和反序列化函数编写单元测试,我不知道如何涵盖FileChooser和FileInputStream 而且,如果这个文件中有两个函数,我必须写两个并且只有两个对应的测试函数 这两项功能如下: /** * serialize and save all the datasets and charts * @param myDataset * pass all the existing datasets into this function for serializ

我需要为序列化和反序列化函数编写单元测试,我不知道如何涵盖FileChooser和FileInputStream

而且,如果这个文件中有两个函数,我必须写两个并且只有两个对应的测试函数

这两项功能如下:

/**
 * serialize and save all the datasets and charts
 * @param myDataset
 *          pass all the existing datasets into this function for serialization purpose
 * @param myChart
 *          pass all the existing charts into this function for serialization purpose
 */

public static void serialize (HashMap<String, DataTable> myDataset, HashMap<String, Chart> myChart) {

    // first, let the user select a directory to save
    FileChooser chooser = new FileChooser();
    chooser.setTitle("Save");
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("comp3111 type", "*.comp3111");
    chooser.getExtensionFilters().add(extFilter);
    File file = chooser.showSaveDialog(new Stage());

    try {
        FileOutputStream fOutput = new FileOutputStream(file);
        ObjectOutputStream objOutput = new ObjectOutputStream(fOutput);

        // put all the DataTable type objects into an array         
        objOutput.writeObject(myDataset);
        objOutput.writeObject(myChart);

        objOutput.close();
        fOutput.close();

    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }

}


/**
 * choose a file with *.comp3111 extension for deserialization purpose
 * @return
 *          the chosen *.comp3111 file
 */
public static File deserialize () {
    FileChooser chooser = new FileChooser();
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("comp3111 type", "*.comp3111");
    chooser.getExtensionFilters().add(extFilter);

    File mfile = chooser.showOpenDialog(new Stage());           
    return mfile;
}

问题是序列化和反序列化方法中的代码不属于那里。它们太宽了:它们不仅序列化和反序列化对象(尽管反序列化代码似乎缺失),而且还打开文件选择器

虽然从程序流的角度来看这可能是有意义的,但它也打破了这一点:我知道SRP通常应用于类而不是方法。你的方法做的事情太多了

因此,您应该以这样的方式编写方法:它们接受一个文件或InputStream作为输入参数,并返回一个对象集合作为反序列化的结果,或者获取一个对象集合并返回一个OutputStream作为序列化的结果。这样,您就可以准确地测试这种行为

更好的方法是首先编写测试,例如,您可以使用测试文件,既可以从中读取以检查它是否生成预期的对象,也可以测试serialiser返回的ByteOutputStream是否与该文件匹配


它被调用,这是编写可测试且稳定的代码的好方法。

问题在于,序列化和反序列化方法中的代码不属于那里。它们太宽了:它们不仅序列化和反序列化对象(尽管反序列化代码似乎缺失),而且还打开文件选择器

虽然从程序流的角度来看这可能是有意义的,但它也打破了这一点:我知道SRP通常应用于类而不是方法。你的方法做的事情太多了

因此,您应该以这样的方式编写方法:它们接受一个文件或InputStream作为输入参数,并返回一个对象集合作为反序列化的结果,或者获取一个对象集合并返回一个OutputStream作为序列化的结果。这样,您就可以准确地测试这种行为

更好的方法是首先编写测试,例如,您可以使用测试文件,既可以从中读取以检查它是否生成预期的对象,也可以测试serialiser返回的ByteOutputStream是否与该文件匹配


它被称为,这是编写可测试和稳定代码的好方法。

是myDataset和myChart域类还是JavaFX类?因为您不应该序列化JavaFX类来实现持久性。myDataset和myChart是域类还是JavaFX类?因为你不应该为了持久性而序列化JavaFX类。非常感谢!采纳建议!谢谢!采纳建议!