The Scenario
 |
This isn't a cliche...
|
I was working on a Java project that talks to a web service with a custom "Client" class that was written internally. In my project, the client is pulled in from a hosted maven repo as a dependency, so I didn't have access to the source (at least not from this project). I'm trying to be better about writing characterization tests for existing code before I go in and change anything (if nothing else it's a good way to feel out the code). But I had a problem...
The Client has a single constructor, which takes an endpoint, a username, and a password. Calling this constructor tries to connect to the endpoint (and crashes if it fails). Because this was the only defined constructor, subclassing wasn't going to help me. It doesn't implement an interface, so that avenue was also closed down.
So there was no obvious way for me to create a test double to pass in to the code I was actually interested in testing. How in the hell was I going to get around this???