如何测试将Java对象列表转换为XML并写入文件的方法?

如何测试将Java对象列表转换为XML并写入文件的方法?,java,xml,unit-testing,Java,Xml,Unit Testing,我有一个方法可以将HotelData对象列表转换为XML并写入文件。说 @Override public void dataToXmlConverter() { /* 1. the method inherited the list `List<HotelData> rows`, so, it doesn't take any arguments as parameter to the method */ DocumentBuilderFactory d

我有一个方法可以将
HotelData
对象列表转换为
XML
并写入文件。说

@Override
public void dataToXmlConverter() {

  /* 1. the method inherited the list `List<HotelData> rows`, 
  so, it doesn't take any arguments as parameter to the method */     



 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = null;

    try {
        docBuilder = docFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("info");
    doc.appendChild(rootElement);

  /* 2. convert the List to the XML format */  
   for (int i = 0; i < rows.size(); i++) {

       // some conversation code 
   }


 /*3. write the XML to the file */ 
  try{
     // write to the XML file 
  }

  catch (Exception ex) {
        ex.printStackTrace();
    }

    System.out.println("CONVERTED TO XML");
}
@覆盖
public void dataToXmlConverter(){
/*1.该方法继承了列表`列表行',
因此,它不接受任何参数作为方法的参数*/
DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=null;
试一试{
docBuilder=docFactory.newDocumentBuilder();
}捕获(ParserConfiguration异常e){
e、 printStackTrace();
}
Document doc=docBuilder.newDocument();
元素rootElement=doc.createElement(“信息”);
doc.appendChild(rootElement);
/*2.将列表转换为XML格式*/
对于(int i=0;i

我在单元测试方面有一些经验,但是,我的问题是我应该在这里测试什么,特别是当没有参数并且返回类型是
void
时。请告诉我。一些示例代码将很有帮助

这里有很多东西要测试

其中一些可以是:

  • 您可以测试消息是否成功写入
  • 您可以编写一些特定的内容,然后读取该文件以查找是否写入了相同的消息

  • 由于该方法不接受任何参数,如何使用列表?对于
    1
    我有一条消息
    “转换为XML”
    ,我可以检查一下。我相信
    2
    会有点难。您可以提供一些示例代码来开始使用吗?您可以阅读以下内容:当它没有参数时,数据从何处转换?我要做的是将类a
    Writer
    对象作为构造函数参数,并在测试中用
    StringWriter
    替换它。我会把数据作为参数传递。然后我可以验证
    StringWriter
    s内容是否是预期的XML。