Capturing Integration Testing Input/Output with XStream
April 8, 2010
Posted by on
A co-worker was looking for a way to capture input and results during a debug session for certain scenarios that were occurring at run time. Ideally they were looking for a tool that could be used in Eclipse and capture parameters and return values from frames in the debug session and then be able to reuse those values in jUnit tests.
A quick search couldn’t find any tools that find the bill, but as a quick and dirty method you can use XStream in an Eclipse “Display” view to dump out input and return variables. Then in a jUnit test you unserialise the XML back into objects and tests the inputs against the results.
The XML format gives you two instant advantages:
- No need to reconstruct the input / result objects from scratch (which can be difficult, especially in complex or legacy systems)
- Easy tweaking of the the XML to get the results you want
A disadvantage is that if the objects structure changes then re-factoring tools will miss the XML data and the test may no longer work.
To use this technique:
- Put XStream in your project classpath
- Show the Display view in eclipse (Widnow->Show View-> Display)
- Put breakpoints in the code to capture input and result objects
- At a breakpoint run the following code in the Display view to serialise the objects to XML
- Save the XML to files and load the files into a jUnit test (say with Commons FileUtils.readFileToString())
If you know of any other tools to do this in a more automated fashion then let me know (it would be cool to do this with a few clicks via a plugin – even generate the test classes).