blob: 55f1b3f43f4cba63c0a7fba4c8f546a67ec25e22 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<meta name="copyright" content="Copyright (c) IBM Corporation and others 2000, 2007. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." >
<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">
<title>
Extracting a new method
</title>
</head>
<body>
<h1>
Extracting 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/editor_testsuite_extract_method.png" alt="Context menu extract method">
</p>
</li>
<li>
In the <strong>Method Name</strong> field, type <em>collectInheritedTests</em>.
<p>
<img src="images/extract_method_dialog.png" 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/extract_method_preview_dialog.png" alt="Extract method preview page">
</p>
</li>
<li>
Go to the extracted method by selecting it in the Outline view.
<p>
<img src="images/editor_testsuite_outline_collectinheritedmethod.png" alt="Extracted method">
</p>
</li>
</ol>
<P class="nav_footer" id="nav_footer">Next Section: <A href="qs-9.htm">Creating a Java class</A>
</P>
<p>
<img src="../images/ngrelc.png" 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/ngrelr.png" alt="Related reference">
</p>
<p>
<a href="../reference/ref-13.htm">Java Preferences</a><br>
</p>
</body>
</html>