July 29, 2004
Mocking a URL

Tulna's having a bit of trouble with some unit testing at the moment. The lovely Emma has pointed out a few holes in our test coverage, and Tulna is endeavouring to fill them.

She's currently working on building tests for our configuration classes. We use Digester to pull in all our configuration from XML. In real life, it's all as sweet as a nut, but obviously we still want unit tests to ensure that any future breakage is discovered PDQ.

We run digester in a ServletContextListener's contextInitialized(). After setting up our digester, after all that addObjectCreate(), addBeanPropertySetter() shenanigans, the code which actually does the digesting looks like this:

URL configFileURL = context.getResource("/WEB-INF/config.xml");
InputStream configFileStream = configFileURL.openStream();
config = (Config) digester.parse(configFileStream);

Question is, how do we get our test data into configFileStream? The context object is a mock, so we can do anything we like there - but how do we mock URL? It's a final class, so we can't sub-class it...

Posted to Java by Simon Brunning at July 29, 2004 05:12 PM
Post a comment

Email Address:



Remember info?