# Extracting a new method

In this section, you will improve the code of the constructor of junit.framework.TestSuite. 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 collectTestMethods.

1. In the junit.framework.TestSuite.java file, select the following range of code inside the TestSuite(Class) constructor:

   `Class superClass= theClass;
   Vector names= new Vector();
   while(Test.class.isAssignableFrom(superClass)) {
   Method[] methods= superClass.getDeclaredMethods();
   for (int i= 0; i < methods.length; i++) {
   addTestMethod(methods[i],names, constructor);
   }
   superClass= superClass.getSuperclass();
   }`

2. From the selection's context menu in the editor, select Refactor > Extract Method....

3. In the Method Name field, type collectInheritedTests.

4. To preview the changes, press Preview>.The preview page displays the changes that will be made. Press OK to extract the method.

5. Go to the extracted method by selecting it in the Outline view.

Java editor
Refactoring support

Java Preferences