Java Reference
In-Depth Information
RequestHandler handler2 = controller.getHandler(request);
assertSame(handler2, handler);
}
@Test
public void testProcessRequest()
{
Response response = controller.processRequest(request);
assertNotNull("Must not return a null response", response);
assertEquals(“Response should be of type SampleResponse”,
SampleResponse.class, response.getClass());
}
}
D
We move the instantiation of the test Request and RequestHandler objects to ini-
tialize B . This saves us from repeating the same code in testAddHandler C and
testProcessRequest D . Also, we make a new @Before annotated method for adding
the handler to the controller. Because @Before methods are executed before every sin-
gle @Test method, we make sure we have a fully set up DefaultController object.
DEFINITION Refactor —To improve the design of existing code. For more
about refactoring, see Martin Fowler's already-classic book, Refactoring: Improv-
ing the Design of Existing Code . 4
Note that you don't try to share the setup code by testing more than one operation in
a test method, as shown in listing 3.9 (an anti-example).
Listing 3.9
Anti-example: don't combine test methods
public class TestDefaultController
{
[...]
@Test
public void testAddAndProcess()
{
Request request = new SampleRequest();
RequestHandler handler = new SampleHandler();
controller.addHandler(request, handler);
RequestHandler handler2 = controller.getHandler(request);
assertEquals(handler2,handler);
// DO NOT COMBINE TEST METHODS THIS WAY
Response response = controller.processRequest(request);
assertNotNull("Must not return a null response", response);
assertEquals(SampleResponse.class, response.getClass());
}
}
4
Martin Fowler, Refactoring: Improving the Design of Existing Code (Reading, MA: Addison-Wesley, 1999).
 
 
Search WWH ::




Custom Search