2
votes

So I'm writing some unit tests using JUnit for an RSS parser application. The part I'm trying to test takes a URL and opens a connection to it (HttpURLConnection), then passes an inputStream of this into a parser (connection.getInputStream()). Once in the parser, creates a new XMLEventReader from the input stream.

I'm trying to replicate this process within my JUnit tests by having a local XML file of the same structure as the RSS feed would be. When I try to load this in using:

this.getClass().getResourceAsStream("/localXmlFile.xml");

I get a MalformedURLException thrown when this line of the parser is hit:

XMLEventReader xmlEventReader = inputFactory.createXMLEventReader(inputStream);

Can anyone shed any light as to how I would correctly replicate the functionality within my tests? Is this something we would use EasyMock for (I don't know as I've never used it).

Thanks in advance.

2
Can you post the stack trace?highlycaffeinated

2 Answers

3
votes

I don't think inputFactory.createXMLEventReader(inputStream); is throwing MalformedURLException because based on Javadoc, that API throws XMLStreamException. It almost seems to me that your JUnit tests have some dangling URL objects that cause MalformedURLException.

1
votes

Could it be that you should have

this.getClass().getResourceAsStream("localXmlFile.xml");