Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit testing with multiple collaborators
    primarykey
    data
    text
    <p>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.</p> <p>However, I end up with a bunch of collaborating objects from Apache Commons:</p> <pre><code>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(); } } </code></pre> <p>I have a <code>PostMethod</code> object, a <code>RequestEntity</code> object and a <code>HttpClient</code> object. I feel relatively comfortable passing in the <code>HttpClient</code> ala dependency injection, but what do I do about the other collaborators?</p> <p>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.</p> <h1>Follow Up</h1> <p>Thanks for the answers! My remaining issue is a method like this:</p> <pre><code>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(); } </code></pre> <p>How do I correctly verify that the returned value is from <code>post.getResponseBodyAsString()</code>? Would I have to mock <code>post</code> as well as <code>client</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload