I had to instrumentalize my code to generate tests. For that purpose, I used serialization to capture my objects state from the server while running some system tests. And I used the generated files for my tests using deserialization.
To tackle the complexity of some object graphs, I choose to use Xstream to generate xml.
Here's how to dump an object to xml:
XStream xstream = new XStream();
String xml = xstream.toXML(cs);
String filePath = "output.xml";
try{
new File(filePath).createNewFile();
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "utf-8"))) {
writer.write(xml);
writer.flush();
}
} catch (IOException e){
e.printStackTrace();
}
I packed it into an utility class:
...
String fpxmlcurrentLossratioContractDTO = "currentLossratioContractDTO.xml";
XmlHelper.writeToFileAsXml(currentLossratioContractDTO,fpxmlcurrentLossratioContractDTO);
...