blob: a34877812155e9b6defefdea51ed1ff469f966c5 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<title>
Extract a new Method
</title>
<link rel="stylesheet" href="../book.css" charset="ISO-8859-1" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
</head>
<body>
<h1>
Extract a new method
</h1>
<p>
In this section, you will improve the code of the constructor of <em>junit.framework.TestSuite</em>.
To make the intent of the code clearer, you will extract the code that collects test cases
from base classes into a new method called <em>collectTestMethods</em>.
</p>
<ol>
<li>
In the <em>junit.framework.TestSuite.java</em> file, select the following range of
code inside the <em>TestSuite(Class)</em> constructor:<br>
<p><code>
Class superClass= theClass;<br>
Vector names= new Vector();<br>
while(Test.class.isAssignableFrom(superClass)) {<br>
&nbsp;&nbsp;&nbsp; Method[] methods= superClass.getDeclaredMethods();<br>
&nbsp;&nbsp;&nbsp; for (int i= 0; i &lt; methods.length; i++) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addTestMethod(methods[i],names, constructor);<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; superClass= superClass.getSuperclass();<br>
}</code></p>
</li>
<li>
From the selection's context menu in the editor, select <strong>Refactor &gt; Extract Method...</strong>.
<p>
<img src="../images/qs-20.gif" width=490 height=577 alt="Context menu extract method">
</p>
</li>
<li>
In the <strong>Method Name</strong> field, type <em>collectInheritedTests</em>.
<p>
<img src="../images/qs-21.gif" width=408 height=444 alt="Extract method wizard data input page">
</p>
</li>
<li>
To preview the changes, press <strong>Preview&gt;</strong>.The preview page displays the
changes that will be made. Press <strong>OK</strong> to extract the method.
<p>
<img src="../images/qs-22.gif" width=444 height=404 alt="Extract method preview page">
</p>
</li>
<li>
Go to the extracted method by selecting it in the Outline view.
<p>
<img src="../images/qs-23.gif" width=537 height=274 alt="Extracted method">
</p>
</li>
</ol>
<p>
<img src="../images/ngrelc.gif" width=159 height=27 border="0" alt="Related concepts">
</p>
<p>
<a href="../concepts/concepts-7.htm">Java editor</a><br>
<a href="../concepts/concepts-9.htm">Refactoring support</a><br>
</p>
<p>
<img src="../images/ngrelt.gif" width=159 height=27 alt="Related tasks" border="0">
</p>
<p>
<a href="../tasks/tasks-54.htm">Using the Java editor</a><br>
<a href="../tasks/tasks-80.htm">Refactoring</a><br>
<a href="../tasks/tasks-83.htm">Refactoring with preview</a><br>
</p>
<p>
<img src="../images/ngrelr.gif" width=159 height=27 alt="Related reference">
</p>
<p>
<a href="../reference/ref-154.htm">Extract Method Errors</a><br>
<a href="../reference/ref-23.htm">Refactoring Preferences</a><br>
</p>
<p>
<a href="../hglegal2003.htm"><img src="../images/ngibmcpy2003.gif" width=324 height=14 alt="Copyright (c) 2000, 2003 IBM Corporation and others. All Rights Reserved." border="0"></a>
</p>
</body>
</html>