|
From: Steve F. <sm...@us...> - 2001-08-27 11:21:23
|
Update of /cvsroot/mockobjects/doc
In directory usw-pr-cvs1:/tmp/cvs-serv14020
Added Files:
another_route.html
Log Message:
Initial copy of new paper
--- NEW FILE: another_route.html ---
<html>
<head>
<title>Another approach to working backwards</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.deemphasised { color: #666666}
h1 { text-align: center; font-family: Arial, Helvetica, sans-serif; font-weight: bold}
h3 { font-family: Arial, Helvetica, sans-serif; font-style: italic; font-weight: bold; font-size: small}
.inline_code { font-family: "Courier New", Courier, mono; font-style: normal; font-size: smaller; vertical-align: middle}
p { font-family: Arial, Helvetica, sans-serif}
li { font-family: Arial, Helvetica, sans-serif }
h2 { font-family: Arial, Helvetica, sans-serif; margin-top: 3%}
-->
</style>
</head>
<body bgcolor="#FFFFFF">
<h1>Another approach to working backwards</h1>
<p align="center">Steve Freeman <tt> <st...@m3...></tt></p>
<h2>Introduction</h2>
<h3>Send a reminder</h3>
<p>The first test assumes success and should check that an email is sent and that
the user is redirected to a success page. How can we test these? Redirects are
managed by the <span class="inline_code">HttpServletResponse</span>, so we use
a mock implementation and tell it to expect to be redirected to the relevant
URL. For the reminder email, we need an object that can verify that it has been
asked to send an email to a given address. We don't know exactly what it will
do just yet, but we do know its inputs and, more or less, when it will get called.
Let's add a placeholder, <span class="inline_code">MockPasswordReminder</span>,
to confirm that our servlet behaves correctly. We also decide to call the servlet
a <span class="inline_code">ForgotPasswordServlet</span>. Now we can write our
first test.</p>
<pre> public void testReminderEmailSent() throws ServletException, IOException {
mockRequest.setupAddParameter(ForgotPasswordServlet.EMAIL_PARAM, EMAIL);
mockReminder.setExpectedEmailAddress(EMAIL);
mockResponse.setExpectedRedirect(SENT_URI + "?email=" + EMAIL);
passwordServlet.doGet(mockRequest, mockResponse);
mockResponse.verify();
mockReminder.verify();
}</pre>
<p>This test says:</p>
<ul>
<li>set the email parameter on the inbound request to a value, <span class="inline_code">EMAIL</span>,
that we can check for later. The <span class="inline_code">mockRequest</span>
is a fake implementation of an <span class="inline_code">HttpServletRequest</span>
that responds as if it were had come from a browser request.</li>
<li>tell our <span class="inline_code">MockPasswordReminder</span> to expect
to be called with the given email address.</li>
<li>tell our <span class="inline_code">mockResponse</span> to expect to be redirected
to the success URL with the email address as a parameter. Again, the <span class="inline_code">mockResponse</span>
is a fake implementation that acts as if it had been supplied by the servlet
engine. </li>
<li>call the servlet object directly, passing in the mock implementations of
the objects it needs; this is where we actually run the code under test. In
this case, we also call the </li>
<li>verify that the expectations we set at the beginning of the test have been
fulfilled.</li>
</ul>
<p>The servlet will be passed the request and response but it has to store the
fake reminder object so, for now, we assign it in a servlet constructor. I will
address how to replace this with the real reminder object later. The first implementation
of the servlet method is:</p>
<pre> protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String emailAddress = request.getParameter(EMAIL_PARAM);
passwordReminder.sendReminder(emailAddress);
response.sendRedirect(SENT_URI + "?" + EMAIL_PARAM + "=" + emailAddress);
}
</pre>
<hr>
<p>© Steve Freeman, 2001</p>
<p> </p>
</body>
</html>
|