We have a URL object in one of our Java classes that we want to mock, but it's a final class so we cannot. We do not want to go a level above, and mock the InputStream because that will still leave us with untested code (we have draconian test coverage standards).
I've tried jMockIt's reflective powers but we work on Macs and there are problems with the Java agent handler that I haven't been able to resolve.
So are there any solutions that do not involve using real URLs in the junit test?
Create a URL-object pointing to the test class itself.
If you don't want to create a wrapper :
Register a URLStreamHandlerFactory
Make the method you want public
Mock the chain
PS : I don't know how to cancel the numbered list auto-spacing after last line
I have used a URLHandler that allows me to load a URL from the classpath. So the following
would open a file named foo from within the class path. To do this, I use a common utility library and register a handler. To use this handler, you just need to call:
and the resource URL is now available.
Does the URL class implement an interface? If so then you could instantiate it using inversion of control or a configurable factory, rather than by direct construction, this would allow you to inject/construct a test instance at test runtime rather than the final instance you currently have.