Today I ran into a very difficult TDD problem. I need to interact with a server through HTTP POSTs. I found the the Apache Commons HttpClient, which does what I need.
However, I end up with a bunch of collaborating objects from Apache Commons:
public void postMessage(String url, String message) throws Exception {
PostMethod post = new PostMethod(url);
RequestEntity entity = new StringRequestEntity(message,
"text/xml; charset=ISO-8859-1");
post.setRequestEntity(entity);
HttpClient httpclient = new HttpClient();
try {
int result = httpclient.executeMethod(post);
System.out.println("Response status code: " + result);
System.out.println("Response body: ");
System.out.println(post.getResponseBodyAsString());
} finally {
post.releaseConnection();
}
}
I have a PostMethod
object, a RequestEntity
object and a HttpClient
object. I feel relatively comfortable passing in the HttpClient
ala dependency injection, but what do I do about the other collaborators?
I could create a bunch of factory methods (or a factory class) to create the collaborators, but I'm a bit afraid that I'd be mocking too much.
Follow Up
Thanks for the answers! My remaining issue is a method like this:
public String postMessage(String url, String message) throws Exception {
PostMethod post = new PostMethod(url);
RequestEntity entity = new StringRequestEntity(message,
"text/xml; charset=ISO-8859-1");
post.setRequestEntity(entity);
HttpClient httpclient = new HttpClient();
httpclient.executeMethod(post);
return post.getResponseBodyAsString();
}
How do I correctly verify that the returned value is from post.getResponseBodyAsString()
? Would I have to mock post
as well as client
?