Java Reference
In-Depth Information
The empty implementation of SampleResponse didn't have a name property you
could test. To get the test you want, you have to implement a little more of the
Response class first. Listing 3.10 shows the enhanced SampleResponse class.
Listing 3.10
A refactored SampleResponse
public class TestDefaultController
{
[...]
private class SampleResponse implements Response
{
private static final String NAME = "Test";
public String getName()
{
return NAME;
}
public boolean equals(Object object)
{
boolean result = false ;
if (object instanceof SampleResponse)
{
result = ((SampleResponse) object).getName().equals(getName());
}
return result;
}
public int hashCode()
{
return NAME.hashCode();
}
}
[...]
Now that SampleResponse has an identity (represented by getName() ) and its own
equals method, you can amend the test method:
@Test
public void testProcessRequest()
{
Response response = controller.processRequest(request);
assertNotNull("Must not return a null response", response);
assertEquals(new SampleResponse(), response);
}
We've introduced the concept of identity in the SampleResponse class for the purpose
of the test. But the tests are telling you that this should have existed in the proper
Response class. You need to modify the Response interface as follows:
public interface Response
{
String getName();
}
As you see, tests can sometimes “talk” and guide you to a better design of your applica-
tion. But this isn't the real purpose of the tests. Don't forget that the tests are used to
 
 
Search WWH ::




Custom Search