title: How to write a unit test for an XML parser I wrote in Java
body: <p>The context is as follows:</p>
  
  <p>I've got objects that represent Tweets (from Twitter). Each object has an id, a date and the id of the original tweet (if there was one).</p>
  
  <p>I receive a file of tweets (where each tweet is in the format of <code>05/04/2014 12:00:00, tweetID, originalID</code> and is in its' own line) and I want to save them as an XML file where each field has its' own tag.</p>
  
  <p>I want to then be able to read the file and return a list of <code>Tweet</code> objects corresponding to the Tweets from the XML file.</p>
  
  <p>After writing the XML parser that does this I want to test that it works correctly. I've got no idea how to test this. </p>
  
  <p>The XML Parser:</p>
  
  <pre><code>public class TweetToXMLConverter implements TweetImporterExporter {
      //there is a single file used for the tweets database
      static final String xmlPath = "src/main/resources/tweetsDataBase.xml";
      //some "defines", as we like to call them ;)
      static final String DB_HEADER = "tweetDataBase";
      static final String TWEET_HEADER = "tweet";
      static final String TWEET_ID_FIELD = "id";
      static final String TWEET_ORIGIN_ID_FIELD = "original tweet";
      static final String TWEET_DATE_FIELD = "tweet date";
      static File xmlFile;
      static boolean initialized = false;
  
      @Override
      public void createDB() {
          try {
              Element tweetDB = new Element(DB_HEADER);
              Document doc = new Document(tweetDB);
              doc.setRootElement(tweetDB);
  
              XMLOutputter xmlOutput = new XMLOutputter();
  
              // display nice nice? WTF does that chinese whacko want?
              xmlOutput.setFormat(Format.getPrettyFormat());
              xmlOutput.output(doc, new FileWriter(xmlPath));
              xmlFile = new File(xmlPath);
              initialized = true;
  
          } catch (IOException io) {
              System.out.println(io.getMessage());
          }
      }
  
      @Override
      public void addTweet(Tweet tweet) {
          if (!initialized) {
              //TODO throw an exception? should not come to pass!
              return;
          }
          SAXBuilder builder = new SAXBuilder();
          try {
              Document document = (Document) builder.build(xmlFile);
  
              Element newTweet = new Element(TWEET_HEADER);
              newTweet.setAttribute(new Attribute(TWEET_ID_FIELD, tweet.getTweetID()));
              newTweet.setAttribute(new Attribute(TWEET_DATE_FIELD, tweet.getDate().toString()));
              if (tweet.isRetweet())
                  newTweet.addContent(new Element(TWEET_ORIGIN_ID_FIELD).setText(tweet.getOriginalTweet()));
  
              document.getRootElement().addContent(newTweet);
  
          } catch (IOException io) {
              System.out.println(io.getMessage());
          } catch (JDOMException jdomex) {
              System.out.println(jdomex.getMessage());
          }
      }
  
      //break glass in case of emergency
      @Override
      public void addListOfTweets(List&lt;Tweet&gt; list) {
          for (Tweet t : list) {
              addTweet(t);
          }
      }
  
      @Override
      public List&lt;Tweet&gt; getListOfTweets() {
          if (!initialized) {
              //TODO throw an exception? should not come to pass!
              return null;
          }
          try {
              SAXBuilder builder = new SAXBuilder();
              Document document;
              document = (Document) builder.build(xmlFile);
  
              List&lt;Tweet&gt; $ = new ArrayList&lt;Tweet&gt;();
              for (Object o : document.getRootElement().getChildren(TWEET_HEADER)) {
                  Element rawTweet = (Element) o;
  
                  String id = rawTweet.getAttributeValue(TWEET_ID_FIELD);
                  String original = rawTweet.getChildText(TWEET_ORIGIN_ID_FIELD);
                  Date date = new Date(rawTweet.getAttributeValue(TWEET_DATE_FIELD));
                  $.add(new Tweet(id, original, date));
              }
              return $;
          } catch (JDOMException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          return null;
      }
  }
  </code></pre>
  
  <p>Some usage:</p>
  
  <pre><code>private TweetImporterExporter converter;
  List&lt;Tweet&gt; tweetList = converter.getListOfTweets();
  for (String tweetString : lines)
      converter.addTweet(new Tweet(tweetString));
  </code></pre>
  
  <p>How can I make sure the the XML file I read (that contains tweets) corresponds to the file I receive (in the form stated above)?</p>
  
  <p>How can I make sure the tweets I add to the file correspond to the ones I tried to add?</p>